【问题标题】:How to check if an instance of NSMutableArray is null or not如何检查 NSMutableArray 的实例是否为空
【发布时间】:2010-01-12 10:03:02
【问题描述】:

如何检查NSMutableArray 是否为空?

【问题讨论】:

    标签: iphone objective-c nsmutablearray nsarray


    【解决方案1】:

    如果要检查是否为空:

    if ([myMutableArray count] == 0) { ... }
    

    如果要检查变量是否为nil

    if (!myMutableArray) { ... }
    

    或:

    if (myMutableArray == nil) { ... }
    

    【讨论】:

    • +1 谢谢你的好回答,我还注意到If([myMutableArray count] > 0) 如果数组中有 nil 或零记录,则返回 false,这意味着 il 数组充当空数组(使用 count 时) )。 :)
    【解决方案2】:
    //if the array has a count of elements greater than 0, then the array contains elements
    if(myarray.count>0){
        NSlog(@"myarray contains values/elements");
    }
    else{ //else the array has no elements
        NSlog(@"myarray is nil");
    }
    

    【讨论】:

    • 如果myarray 是空的,但不是nil,那么它仍然会落入else 分支。
    【解决方案3】:

    有多种检查方法。

    1. if (array == [NSNull null]) { //我的数组为空 }
    2. 如果(数组.count==0) { //我的数组为空 }
    3. 如果(数组 == 零) { //我的数组是空白的 }

    【讨论】:

      【解决方案4】:

      你也可以这样检查...

      if self.yourMutableArray.count == 0 {
           // Your Mutable array is empty. 
      } else {
          // Your Mutable array is not empty.
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-07
        • 1970-01-01
        • 2011-02-12
        • 2011-03-26
        • 1970-01-01
        • 1970-01-01
        • 2017-11-15
        相关资源
        最近更新 更多