如何在Solidity中返回映射列表? (以太坊合约) [英] How to return mapping list in Solidity? (Ethereum contract)
本文介绍了如何在Solidity中返回映射列表? (以太坊合约)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想制作一个具有列表,可以设置项目并可以获取列表的简单智能合约。
I want to make simple smart contract that has a list, can set item, and can get the list.
坚固的代码:
contract lister {
mapping(int => string) list;
int id = 0;
function getList() returns ( /*HERE*/ ) {
return list;
}
function setItemToList(string str) {
list[id] = str;
id++;
}
}
我想让getList()返回列表,但是返回类型不兼容。
我该怎么做?
I want to make getList() return the list, but return type is not compatible. How can I do that?
推荐答案
批量访问列表/数组/等在Solidity中很痛苦。您很少在合同中看到它。在您的情况下,可能的解决方案是提供一个函数,使用其索引访问一个项,并使调用者从0循环到id。
Bulk access to lists/arrays/etc is painful in Solidity. You rarely see it in contracts. In your case, a possible solution is to provide a function to access one item, using its index, and to let the caller loops from 0 to id.
这篇关于如何在Solidity中返回映射列表? (以太坊合约)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文