60

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

Jaywant Khedkar
  • 5,941
  • 2
  • 44
  • 55
Shishir.bobby
  • 10,994
  • 21
  • 71
  • 100

12 Answers12

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

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

Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • 5
    It works but it's not completely flawless: `array.count` should be `[array count]` since you're not dealing with a property (var) here. – Rengers Oct 18 '10 at 17:15
  • 6
    Nope -- `array.count` is just fine in that context. Syntactically, anyway. Stylistically? No particular standard is recommended at this time. – bbum Oct 18 '10 at 17:22
  • 4
    this is the same as `if (!array.count)` – user102008 Apr 18 '12 at 01:20
  • 2
    I think the weight of stylistic opinion is against dot notation for calling generic methods on an object, so I'm with @Rengers on this one. – davidf2281 May 09 '13 at 11:51
  • 1
    For the sake of accuracy @Rengers and @davidf2281, NSArray.h declares count as a property `@property (readonly) NSUInteger count;` – nicktmro Apr 21 '15 at 03:57
  • For those, !array.count----> is giving this error-[NSNull count]: unrecognized selector sent to instance, try @Sachin's array == (id)[NSNull null] – mavericks Oct 06 '15 at 11:58
29
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.

Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
Andy Riordan
  • 1,154
  • 1
  • 8
  • 13
12

you can try like this

if ([array count] == 0)
Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
Vanya
  • 4,973
  • 5
  • 32
  • 57
9

Best performance.

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

The way to go with big arrays.

quarac
  • 3,454
  • 3
  • 21
  • 25
  • 3
    best answer. the others suggest to use `count` property, which implies enumerating the array and can be a performance issue with big arrays – boweidmann May 14 '18 at 13:25
  • I prefer this way as well, because we don't actually care about the exact count when we just want to check whether array is empty. – Kjuly Jul 15 '21 at 00:36
9

Just to be really verbose :)

if (array == nil || array.count == 0)
willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111
4
if (array == (id)[NSNull null] || [array count] == 0) {
    NSLog(@"array is empty");
}
itsji10dra
  • 4,603
  • 3
  • 39
  • 59
SachinVsSachin
  • 6,401
  • 3
  • 33
  • 39
3

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

Community
  • 1
  • 1
Abdul Karim
  • 4,359
  • 1
  • 40
  • 55
2

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

if (![array count])

the '==' operator is not necessary.

Brian
  • 30,156
  • 15
  • 86
  • 87
1

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.

1

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");
}
JasonLee
  • 38
  • 5
0
if (array == nil || array.count == 0 || [array isEqaul [NSNull Null]])
Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73
saurabh rathod
  • 1,090
  • 7
  • 7
0

In Swift 4

if (array.isEmpty) {
    print("Array is empty")
}
else{
    print("Array is not empty")
}
Harshal Bhavsar
  • 1,598
  • 15
  • 35
Raghib Arshi
  • 717
  • 8
  • 12