How to check if array is null or empty?

IphoneObjective C

Iphone Problem Overview


I want to check if my array is empty or null, and on base of which I want to create a condition for example.

if(array ==  EMPTY){
//do something
}

I hope I'm clear what I am asking, just need to check if my array is empty?

regards

Iphone Solutions


Solution 1 - Iphone

if (!array || !array.count){
  ...
}

That checks if array is not nil, and if not - check if it is not empty.

Solution 2 - Iphone

if ([array count] == 0)

If the array is nil, it will be 0 as well, as nil maps to 0; therefore checking whether the array exists is unnecessary.

Also, you shouldn't use array.count as some suggested. It may -work-, but it's not a property, and will drive anyone who reads your code nuts if they know the difference between a property and a method.

UPDATE: Yes, I'm aware that years later, count is now officially a property.

Solution 3 - Iphone

you can try like this

if ([array count] == 0)

Solution 4 - Iphone

Best performance.

if (array.firstObject == nil)
{
    // The array is empty
}

The way to go with big arrays.

Solution 5 - Iphone

Just to be really verbose :)

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

Solution 6 - Iphone

if (array == (id)[NSNull null] || [array count] == 0) {
    NSLog(@"array is empty");
}

Solution 7 - Iphone

Swift 3

As in latest version of swift 3 the ability to compare optionals with > and < is not avaliable

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!")
}

as per array count

if array?.count ?? 0 > 0 {
    print("There are objects!")
}

There are other ways also and can be checked here link to the answer

Solution 8 - Iphone

As nil maps to 0, which equals NO, the most elegant way should be

if (![array count])

the '==' operator is not necessary.

Solution 9 - Iphone

You can also do this kind of test using if (nrow>0). If your data object is not formally an array, it may work better.

Solution 10 - Iphone

null and empty are not the same things , i suggest you treat them in differently

if (array == [NSNull null]) {
    NSLog(@"It's null");
} else if (array == nil || [array count] == 0) {
     NSLog(@"It's empty");
}

Solution 11 - Iphone

if (array == nil || array.count == 0 || [array isEqaul [NSNull Null]])

Solution 12 - Iphone

In Swift 4

if (array.isEmpty) {
    print("Array is empty")
}
else{
    print("Array is not empty")
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShishir.bobbyView Question on Stackoverflow
Solution 1 - IphoneVladimirView Answer on Stackoverflow
Solution 2 - IphoneAndy RiordanView Answer on Stackoverflow
Solution 3 - IphoneVanyaView Answer on Stackoverflow
Solution 4 - IphonequaracView Answer on Stackoverflow
Solution 5 - IphonewillcodejavaforfoodView Answer on Stackoverflow
Solution 6 - IphoneSachinVsSachinView Answer on Stackoverflow
Solution 7 - IphoneAbdul KarimView Answer on Stackoverflow
Solution 8 - IphoneBrianView Answer on Stackoverflow
Solution 9 - IphoneconservationistaView Answer on Stackoverflow
Solution 10 - IphoneJasonLeeView Answer on Stackoverflow
Solution 11 - Iphonesaurabh rathodView Answer on Stackoverflow
Solution 12 - IphoneRaghib ArshiView Answer on Stackoverflow