Solidity

賦值後如何清空uint數組?

  • February 17, 2021

這是uint智能合約中定義的數組:

uint256[] public bidderItemID;

並且bidderItemID在契約中被賦值:

bidderItemID = [1,2,3];

bidderItemID賦值後如何清空?也就是說,bidderItemID.lengthis0中沒有元素bidderItemID。我試過bidderItemID=[]了,它拋出了一個錯誤。

你可以通過delete一個數組來實現你的目標。請參閱以下程式碼作為範例。

pragma solidity 0.6.12;


contract Test {
   uint256[] public bidderItemID;
   
   function setBidderItemID() public {
       bidderItemID = [1,2,3];
   }
   
   function deleteBidderItemID() public {
       delete bidderItemID;
   }
}

引用自:https://ethereum.stackexchange.com/questions/93574