如何检查NSMutableArray的实例是否为null

时间:2010-01-12 10:03:02

标签: iphone objective-c nsmutablearray nsarray

如何检查NSMutableArray是否为空?

4 个答案:

答案 0 :(得分:77)

如果你想检查它是否为空:

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

如果您想检查变量是否为nil

if (!myMutableArray) { ... }

或:

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

答案 1 :(得分:0)

//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");
}

答案 2 :(得分:0)

有多种方法可以检查它。

  1. if (array == [NSNull null])
    {
        //myarray is blank
    }
    
  2. if(array.count==0)
    {
        //myarray is blank
    }
    
  3. if(array == nil)
    {
        //my array is blank
    }
    

答案 3 :(得分:0)

你也可以这样检查......

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