【问题标题】:Push string into array in struct in solidity将字符串推入结构中的数组中
【发布时间】:2021-08-18 06:31:30
【问题描述】:
我有一个 Solidity 结构,定义如下:
struct Batch{
address payable owner;
address payable[] precedentsOwners;
uint[] precedentsBatches;
}
我想创建一个函数,允许我将所有者列表附加到这个结构,但我得到了很多错误......有什么办法吗?
非常感谢。
【问题讨论】:
标签:
arrays
function
struct
solidity
【解决方案1】:
您可以使用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));
}
}