如何在 solidity 中初始化静态字符串数组?

How to initialize a static string array in solidity?

我想声明一个数组来存储我的字符串,如下所示。

bytes32[3] verifiedResult;
verifiedResult = ["verified successfully", "verified failed", "not clear"];

错误表明

ParserError: Expected identifier but got '=' --> contracts/1_Storage.sol:23:20: | 23 | verifiedResult = ["verified_successfully", "verified_failed", "not clear"] |^

我应该怎么做才能解决它?有没有更好的方法在 solidity 中声明静态字符串数组?

您可以使用 string 关键字,或者在您的情况下,使用固定大小 (3) 的字符串数组。

pragma solidity ^0.8.4;

contract MyContract {
    // string in storage
    string[3] verifiedResult = ["verified successfully", "verified failed", "not clear"];
    
    function foo() external {
        // string in memory
        string[3] memory anotherStringArray = ["verified successfully", "verified failed", "not clear"];
    }
}

此外,您的副本看起来可以使用 enum

pragma solidity ^0.8.4;

contract MyContract {
    enum VerifiedResult {VerifiedSuccessfully, VerifiedFailed, NotClear}
    
    VerifiedResult result;
    
    function foo() external {
        result = VerifiedResult.NotClear; // stores `NotClear` to the `result` property
    }
}