Solidity 函数returns多个值的接收方式 总结

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

函数返回多个值

// 函数可以返回多个值
function multipleReturns() returns(uint a, uint b, uint c) {
  return (1, 2, 3);
}

同时接收所有返回值

function processMultipleReturns() {
  uint a;
  uint b;
  uint c;
  // 这样来做批量赋值:
  (a, b, c) = multipleReturns();
}

只接收部分返回值

// 或者如果我们只想返回其中一个变量:
function getLastReturnValue() {
  uint c;
  // 可以对其他字段留空:
  (,,c) = multipleReturns();
}

项目中实际碰到问题

 function multipleReturns() returns(string memory a, uint b, uint c) {
      return ("1", 2, 3);
    }
    
    function processMultipleReturns() {
      string memory a;
      uint b;
      uint c;
      // 这样来做批量赋值:
      (a, b, c) = multipleReturns();
    }
 mapping (uint256 => Tree) public dnaToTree;
 
  //通过 Dna 获取加密资产详情 
    function getTreeByDna(uint256 _Dna) public constant returns(string memory treeByDna_user , uint256 treeByDna_dna,string memory treeByDna_treeName, uint64 treeByDna_birthTime, uint256 treeByDna_needFooder, bytes32  treeByDna_status, bool treeByDna_isAdult) {
        return (dnaToTree[_Dna].user, dnaToTree[_Dna].dna, dnaToTree[_Dna].treeName, dnaToTree[_Dna].birthTime, dnaToTree[_Dna].needFooder ,dnaToTree[_Dna].status ,dnaToTree[_Dna].isAdult);
    }
    
    function getTreeByDna_d(uint256 _Dna) public constant {
       
        string memory treeByDna_user;
        uint256 treeByDna_dna;
        string memory treeByDna_treeName;
        uint64 treeByDna_birthTime;
        uint256 treeByDna_needFooder;
        bytes32 treeByDna_status;
        bool treeByDna_isAdult;
        (treeByDna_user, treeByDna_dna, treeByDna_treeName, treeByDna_birthTime, treeByDna_needFooder, treeByDna_status, treeByDna_isAdult) = getTreeByDna(_Dna);
        
    }

转载于:https://my.oschina.net/u/3734107/blog/1837526

你可能感兴趣的:(Solidity 函数returns多个值的接收方式 总结)