将 String [ ] 传递给 Solidity 中的构造函数

Pass String [ ] to constructor in Solidity

我正在使用 remix IDE 来部署智能合约,我正在传递一个字符串 [],其中将包含像这样的候选人姓名 ["alice","bob"] ....

这是我的智能合约

pragma solidity ^0.4.18;
// We have to specify what version of compiler this code will compile with

contract Voting {
  /* mapping field below is equivalent to an associative array or hash.
  The key of the mapping is candidate name stored as type bytes32 and value is
  an unsigned integer to store the vote count
  */

  mapping (bytes32 => uint8) public votesReceived;

  /* Solidity doesn't let you pass in an array of strings in the constructor (yet).
  We will use an array of bytes32 instead to store the list of candidates
  */

  bytes32[] public candidateList;


  /* This is the constructor which will be called once when you
  deploy the contract to the blockchain. When we deploy the contract,
  we will pass an array of candidates who will be contesting in the election
  */
  function Voting(string[] candidateNames) public {
        for(uint i = 0; i < candidateNames.length; i++) {
            candidateList[i]= stringToBytes32(candidateNames[i]);

        }

 }
  function totalVotesFor(bytes32 candidate) view public returns (uint8) {

    return votesReceived[candidate];
  }

  function stringToBytes32(string memory source) returns (bytes32 result) {
    bytes memory tempEmptyStringTest = bytes(source);
    if (tempEmptyStringTest.length == 0) {
        return 0x0;
    }

    assembly {
        result := mload(add(source, 32))
    }
}


  function voteForCandidate(bytes32 candidate) public {

    votesReceived[candidate] += 1;
  }


}

但我遇到了这个错误,我不知道如何解决

UnimplementedFeatureError: Nested arrays not yet implemented.

谁能帮帮我

如错误所示,Solidity 不支持传入数组数组(string 只是一个字节数组)。

来自Solidity docs

Is it possible to return an array of strings (string[]) from a Solidity function?

Not yet, as this requires two levels of dynamic arrays (string is a dynamic array itself).

您可以做的是将其更改为 bytes32 数组(字符串)并为您的参数发送十六进制数组:

pragma solidity ^0.4.19;

contract Names {
    bytes32[] public names;
    
    function Names(bytes32[] _names) public {
        names = _names;
    }
    
    function get(uint i) public constant returns (bytes32) {
        return names[i];
    }
}

部署

["0x616c696365000000000000000000000000000000000000000000000000000000","0x626f620000000000000000000000000000000000000000000000000000000000"]

你不能在 solidity 中传递一个 String[] 但你可以传递多个字符串! 我的想法是传递您的字符串 (string1 string2 ,string3 ,string4....) 然后使用此函数将字符串转换为 byte32

function stringToBytes32(string memory source)view public returns (bytes32 result) {
    bytes memory tempEmptyStringTest = bytes(source);
    if (tempEmptyStringTest.length == 0) {
        return 0x0;
    }

    assembly {
        result := mload(add(source, 32))
    }
}

然后将转换后的 stringtobyte32 放入 Byte32[] !

你可以试试

设置:

function something(bytes32[] _thing) public{
  for(i=0; i<Array.length ; i++){
    StructArray.push({ thing = _thing[i]});
  }
}

得到:

function getThing()
    public
    returns (bytes32[] memory)
{
    bytes32[] memory addrs = new address[](indexes.length);        
    for (uint i = 0; i < StructArray.length; i++) {
        Struct storage structs = StructArray[array[i]];
        thing[i] = structs.thing;    
    }
    return (thing);
}

更正此问题,因为这是为回答目的而立即编写的

这里是 2022 年,是的,现在是。在 Remix 中打开它并使用类似 ["str1", "str2"] 的东西来部署:

pragma solidity ^0.8.7;

contract Strings  {  
    string[] public stringParam;
   
    constructor(string[] memory _stringParam) {
        stringParam = _stringParam;
    }
}