Solidity
如何設置結構內部的映射值?
我已經嘗試了一些方法,但它不起作用,並且在網際網路上得到了任何有效的例子。
struct customerId{ address id; mapping(string=>uint) customerAmount; mapping(string=>uint) customerPayed; bytes customer_address; } function add() public { cosId memory a= cosId({ id:msg.sender, cosAmount:123, cosPayed:123, customer_address:"abcd" }); }
如果您使用的是儲存
customerId
結構的映射,您可以看到這個智能合約範例:// SPDX-License-Identifier: MIT pragma solidity ^0.8.10; contract Test { struct customerId{ address id; mapping(string=>uint) customerAmount; mapping(string=>uint) customerPayed; bytes customer_address; } mapping(address=>customerId) _arrayCustomerId; function add() public { customerId storage customer = _arrayCustomerId[msg.sender]; customer.id = address(msg.sender); customer.customerAmount["test"] = 1; customer.customerPayed["test1"] = 2; customer.customer_address = abi.encodePacked(msg.sender); } function getElement() external view returns(address, uint, uint, bytes memory){ return (_arrayCustomerId[msg.sender].id, _arrayCustomerId[msg.sender].customerAmount["test"], _arrayCustomerId[msg.sender].customerPayed["test1"], _arrayCustomerId[msg.sender].customer_address); } }