【问题标题】:how to check if array is null or empty in ios sdk?如何在ios sdk中检查数组是否为空或空?
【发布时间】:2014-12-16 12:52:41
【问题描述】:

我想检查我的 Bond 是否为空或 null 和 NULL 值。

{
Bonds =(
      {
        DOB = "12/09/1988";
        about = "";
      }
      );
User =     {
    city = CA;
    email = "karmhadadmtl@gmail.com";
};
success = True;
}

此类型第二次获取数据如何查看Bond key

{
Bonds = Null;
User =     {
    city = CA;
    email = "developer.i12@gmail.com";
};
success = True;
}

【问题讨论】:

标签: ios iphone xcode


【解决方案1】:

您只需检查nil

if(data[@"Bonds"]==nil){
  NSLog(@"it is nil");
}

if ([data[@"Bonds"] isKindOfClass:[NSNull class]]) {
    NSLog(@"it is null");
}

【讨论】:

    【解决方案2】:
    if ([data[@"Bonds"] isKindOfClass:[NSNull class]] || data[@"Bonds"] == nil || [data[@"Bonds"] isEqualToString:@""]) {
    
    }
    

    【讨论】:

      【解决方案3】:

      [NSNull null] 总是返回同一个对象,所以这应该可以正常工作。

      if (dictionary[@"Bonds"] != [NSNull null]) {
          // requried data is present, now check if it is nil or empty.
      }
      

      【讨论】:

      • 我认为解决方案是用错误的实现语言提供的。 Null 与 NSNUll 不同。
      • 完美答案和检查 NSNull 对象的推荐方法。
      • @JohanKarlsson:很明显,您看到的输出来自 NSLog 语句,这就是 NSLog 记录 NSNull 对象的方式。您的评论完全错误。
      • 嗯...查看问题没有任何迹象表明这是 NSLog 的输出。这显然是 JSON 输出。但是查看标签我会注意到这是Objective-C。所以我的错。
      【解决方案4】:

      检查:if (![dataArray isKindOfClass:[NSNull class]]) &&

      检查它是否有元素 [dataArray firstObject] - 检查数组是否有一个或多个元素。

      【讨论】:

        【解决方案5】:

        最好的办法应该是:

        if (!data[@"Bonds"] || ![data[@"Bonds"] count]){
            NSLog(@"Data Found");
        }else{
            NSLog(@"Data Not Found");
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-11-04
          • 1970-01-01
          • 2022-10-20
          • 2011-01-23
          • 1970-01-01
          • 2012-07-29
          相关资源
          最近更新 更多