我正在使用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.
任何人都可以帮助我PLZ
答案 0 :(得分:1)
你不能传递一个坚固的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 []!
答案 1 :(得分:1)
您可以尝试
设置:
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);
}
更正此内容,因为它是为回答目的而立即编写的