3

例如 - 具有命名索引或哈希的数组。类似于 PHP 代码的东西;

$array = array(
"foo" => "some foo value",
"bar" => "some bar value",);

谢谢

4

2 回答 2

3

Solidity 支持从现有类型创建新类型的结构。这些可以在内部函数之间传递,但目前还不能在合约和外部世界之间传递。

只需添加 Jacob 所说的,您可以将结构存储在映射(哈希表)中:

mapping(address => MyStruct) structsInMap;

或在数组中:

MyStruct[] structsInList;

这是一个小例子,它通过遵守与 ABI 兼容的固定大小规则传递值。

contract StructExample {

    struct MyStruct {
        bool isMyStruct;
        uint amount;
        address[3] addressList;
        bytes32 name;
    }

    // storage instance of MyStruct
    MyStruct myStruct;

    function StructExample(
        uint amount, 
        address address1, 
        address address2, 
        address address3,
        bytes32 name) 
    {
        myStruct.isMyStruct = true;
        myStruct.amount = amount;
        myStruct.addressList = [address1, address2, address3];
        myStruct.name = name;
    }

    function getMyStruct() 
      constant
      returns(
          bool isMyStruct,
          uint amount,
          address[3] addressList,
          bytes32 name)
    {
        return (
            myStruct.isMyStruct, 
            myStruct.amount, 
            myStruct.addressList, 
            myStruct.name);
    }
}

并且 Browser Solidity 显示在构造函数设置它们后返回的 struct vals。

在此处输入图像描述

希望能帮助到你。

于 2017-02-03T03:51:32.900 回答
2

Solidity 支持一种称为映射的类型:

contract MappingExample {
    mapping(address => uint) public balances;

    function update(uint newBalance) {
        balances[msg.sender] = newBalance;
    }
}

http://solidity.readthedocs.io/en/develop/types.html#mappings

于 2017-01-27T16:44:01.447 回答