Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check NSArray is null or empty in iOS?

Tags:

ios

iphone

ipad

People also ask

How check array is empty or not in Swift?

To check if an array is empty, use Swift function isEmpty on the array. Following is a quick example, to check if an array is empty. The function returns a boolean value. If the array is empty, isEmpty returns true, else false.

How do you check if an array is empty in Objective C?

It is still possible to compare optionals with ==, so the best way to check if an optional array contains values is: if array?. isEmpty == false { print("There are objects!") }


if (array == nil || [array count] == 0) {
    ...
}

NSArray has the count method, a common way to do it would be...

if (![self.myArray count])
{
}

That will check if the array has nothing in it, or if it is set to nil.


While we are all throwing out the same answers, I thought I would too.

if ([array count] < 1) {
    ...
}

and another

if(!array || array.count==0)

if([myarray count]) It checks for both not empty and nil array.