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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With