How to check if an instance of NSMutableArray is null or not

最后都变了- 提交于 2019-12-18 12:10:48

问题


How to check an NSMutableArray is null or not ?


回答1:


If you want to check if it is empty:

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

If you want to check if the variable is nil:

if (!myMutableArray) { ... }

or:

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



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



回答3:


There are multiple ways to check it.

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



回答4:


You can check this way also...

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


来源:https://stackoverflow.com/questions/2048048/how-to-check-if-an-instance-of-nsmutablearray-is-null-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!