问题描述
我正在尝试在以太坊网络智能合约上制作一个简单的“文本编辑器”。我想做一个数据输入函数,它收集一个STRING中的文本和一个收集文本位置的BYTES10变量。您必须整齐地存储文本,以便以后可以访问它。您应该能够在不替换旧文本的情况下输入新文本。然后是一个输出函数,返回文本和坐标,所有这些都是自合约开始以来进入网络的。以及允许您删除数据的功能,以防输入错误。到目前为止,我有这个代码:
pragma solidity <0.9.0;
contract texteditor {
struct book {
string block;
bytES10 coordinates;
}
book [] public books;
function save(string calldata _blocks,bytES10 _coordinates) public{
books.push(book(_blocks,_coordinates));
}
function read()view public returns (string){
return books[_block][_coordinates];
}
function remove(string _blocks,bytES10 _coordinates) private {
delete book[_blocks][_coordinates];
}}
保存文本的功能我觉得还可以,但是另外两个我编译有问题,不知道是编译器版本的原因还是功能不对。我在博客和其他有关此主题的帮助中找到的信息显然在编译器版本方面已经过时,并且往往会给我带来问题。我想我必须做一个映射,但我还没有找到方法。我感谢您能给我的任何帮助,以推动我在这方面取得进展。非常感谢您抽出宝贵时间。
解决方法
pragma solidity ^0.8.4;
// SPDX-License-Identifier: MIT
contract texteditor {
uint256 public id=0;
struct book {
string block;
string coordinates;
}
mapping(uint256=>mapping(uint256 => book)) bookStore;
mapping(uint256=>uint256) bookBlockIndex;
function save(uint256 bookId,string calldata _block,string calldata _coordinates) public{
book memory temp_book = book(_block,_coordinates);
bookStore[bookId][bookBlockIndex[bookId]]=temp_book;
bookBlockIndex[bookId]++;
}
function read_book(uint256 bookid,uint256 bookBlockid)view public returns (string memory,string memory){
return (bookStore[bookid][bookBlockid].block,bookStore[bookid][bookBlockid].coordinates);
}
function remove_book(uint bookid,uint256 bookBlockid) public {
delete bookStore[bookid][bookBlockid];
}
}
这可能有帮助