牢固地存在像零东西一样的东西吗 [英] Are there null like thing in solidity

查看:226
本文介绍了牢固地存在像零东西一样的东西吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    struct buyer{
       uint amount;
       Status status;
    }

    mapping(address=>buyer) public buyers;
    mapping(uint=>address) buyerIndex;
    uint public buyerNum;
    //Order a product.
    function(){
      uint doubleValue=value*2;
      uint amount=msg.value/doubleValue; 
      if(buyers[msg.sender]==null){ //Error in this line
      buyer abuyer=buyer({amount:amount,status:Status.Created}); //Error in this line
      buyerNum++;
      buyerIndex[buyerNum]=msg.sender;
      buyers[msg.sender]=abuyer;
    }else{
      buyers[msg.sender].amount+=amount;
    }
      Order(msg.sender,amount*doubleValue,amount);

 }

如果没有在买方映射中记录买方,则BuyerNum ++; 但我不知道如何判断买家是否在地图中

If a buyer is not recorded in the buyer mapping, then buyerNum++; but I don't know how to tell whether a buyer is in the mapping

推荐答案

对于整数:

您可以创建none变量以将其用作NULL:

For integers:

You could create none variable to use it as a NULL:

uint256 constant NULL = 0;

检查示例代码:

function isNULL(uint256 variable) internal returns (bool) {
    return variable == NULL;
}


对于bytes32:

对于bytes,您可以采用其他方法:


For bytes32:

You can follow different approach for bytes:

bytes32 constant NULL = "";

示例代码段:

pragma solidity ^0.6.0;

mapping(address => bytes32) public Countries;   

function isCountriesInitialized(address _user) external view returns (bool) 
{
    if (Countries[_user] == NULL) // Returns true if `Countries[_user]` is not initialized
        return false;

    return true;
}


我观察到,在稳固性> = v0.6.0上,即使未映射,它的长度也可能返回32.


I observe that on solidity >= v0.6.0 it may return 32 for length even though it is not mapped.

其返回值的示例:

b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

这篇关于牢固地存在像零东西一样的东西吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆