2010-01-12 20 views

回答

75

如果你想檢查它是否是空的:

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

如果你想檢查變量nil

if (!myMutableArray) { ... } 

或:

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

+1偉大的答案! – swiftBoy 2012-08-14 07:31:02

+1

+1謝謝你的回答,我也注意到如果數組中有零或零記錄,If([myMutableArray count]> 0)'將返回false,這意味着il數組作爲一個空數組(當count是用過的)。 :) – Gram 2015-07-03 21:03:49

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

如果'myarray'是空的,但不是零,那麼它仍然會落入'else'分支。 – freespace 2015-07-04 11:51:05

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 
    } 
    
0

您可以檢查此方式也...

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