【问题标题】:iPhone Check Empty dictionaryiPhone 检查空字典
【发布时间】:2012-06-21 02:56:54
【问题描述】:
if ([dict objectForKey:@"photo"] !=(id)[NSNull null]) 
    {NSLOG(@"dictinary is not empty")}

这对我不起作用。检查空标签

【问题讨论】:

  • 照片键是字符串还是数值?
  • 您将NSNullNULL 混淆了。前者用于将空对象插入到以空结尾的数组中,而后者用于检查对象是否存在。

标签: iphone ios dictionary


【解决方案1】:

这对我有用。您可以使用此代码处理 Null。

[dict isKindOfClass:[NSNull class]];

【讨论】:

    【解决方案2】:

    我已经接受了这样的字符串:

     NSString *strPhoto=[dict objectForKey:@"photo"];
    
    if (strPhoto ==(id)[NSNull null] || [strPhoto isEqualToString:@""] || strPhoto==nil || [strPhoto length]==0) {
    }
    

    它对我有用!!!!!!!!!!

    【讨论】:

      【解决方案3】:

      使用count

      例如:

      if ([dict count] == 0) {
           NSLog("empty");
      }
      

      如果要检查密钥,则:

      if([dict objectForKey:@"photo"]) {
          NSLog(@"There's an object in photo");
      }
      

      【讨论】:

        【解决方案4】:

        试试这段代码检查是否为空值

        NSString *value=[dict objectForKey:@"photo"];
        
        if (value.length != 0 )
        {
          NSLog("not empty!");
        }
        else
        {
          NSLog("empty!");
        }
        

        【讨论】:

          【解决方案5】:

          如果objectForKey:@"photo" 不存在时为空,您可以这样做:if ([dictionary objectForKey:@"photo"]) {NSLog(@"dictionary is not empty);}

          【讨论】:

          • 是的。这是执行if 语句的最佳实践方式。
          猜你喜欢
          • 2011-11-01
          • 1970-01-01
          • 2020-06-10
          • 1970-01-01
          • 2022-07-21
          • 2013-11-23
          • 1970-01-01
          • 2018-09-28
          • 2019-04-19
          相关资源
          最近更新 更多