Ethereum “如何返回”;空";或一个;“空的”;物体是否坚固?

Ethereum “如何返回”;空";或一个;“空的”;物体是否坚固?,ethereum,solidity,smartcontracts,Ethereum,Solidity,Smartcontracts,我目前正在Solidity中编写一份智能合同。除其他信息外,智能合约在一般级别存储对象属性数组。属性对象如下所示: struct PropertyObj { string id; uint weiPrice; address owner; } 现在有一个特定的函数在数组上迭代,找到属性并返回它(下面的代码) 函数getPropertyByid(字符串内存\u propertyId)私有视图返回(PropertyObj内存){ 对于(uint i=0;iSolid

我目前正在Solidity中编写一份智能合同。除其他信息外,智能合约在一般级别存储对象属性数组。属性对象如下所示:

    struct PropertyObj {
    string id;
    uint weiPrice;
    address owner;
}
现在有一个特定的函数在数组上迭代,找到属性并返回它(下面的代码)

函数getPropertyByid(字符串内存\u propertyId)私有视图返回(PropertyObj内存){

对于(uint i=0;iSolidity没有
null
值,正如您正确指出的那样

您的函数可以使用该函数引发异常

您的实现似乎还存在逻辑错误。如果在第一次迭代中未找到哈希,您的示例将“返回null”。相反,您可能希望在循环结束后引发异常

for(uint i = 0; i<PropertyArray.length; i++){
    if (keccak256(bytes((PropertyArray[i].id))) == keccak256(bytes((_propertyId)))) {
        return PropertyArray[i];
    }
}

revert('Not found');

for(uint i=0;iYes,关于我返回“null”的位置,您是对的,谢谢您指出:)。另一方面,如果我理解正确,没有返回“null”的“默认”方法,但是在我的情况下,我可以声明一个“空”对象并返回它。
for(uint i = 0; i<PropertyArray.length; i++){
    if (keccak256(bytes((PropertyArray[i].id))) == keccak256(bytes((_propertyId)))) {
        return PropertyArray[i];
    }
}

revert('Not found');
for(uint i = 0; i<PropertyArray.length; i++) {
    // ...
}

// not found, return empty `PropertyObj`
PropertyObj memory emptyPropertyObj;
return emptyPropertyObj;