将字符串推入结构中的数组中

Push string into array in struct in solidity

我有一个 solidity 结构定义如下:

struct Batch{
    address payable owner;
    address payable[] precedentsOwners;
    uint[] precedentsBatches;
}

我想创建一个允许我将所有者列表附加到该结构的函数,但是我遇到了很多错误...有什么办法可以做到这一点吗?

非常感谢。

您可以使用 push() 数组方法将项目添加到存储数组中。

请注意,您的数组是 address payable 类型,因此如果您要传递常规 address(请参阅 appendToOwners() 的参数),则需要将其转换为 payable第一。

pragma solidity ^0.8;

contract MyContract {
    struct Batch{
        address payable owner;
        address payable[] precedentsOwners;
        uint[] precedentsBatches;
    }
    
    Batch public myBatch;
    
    function appendToOwners(address _newOwner) external {
        myBatch.precedentsOwners.push(payable(_newOwner));
    }
}