views:

89

answers:

4

Hi all,

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

+3  A: 

you can try like this

if ([array count] == 0)

Vanya
+7  A: 
if (!array || !array.count){
  ...
}

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

Vladimir
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
Nope -- `array.count` is just fine in that context. Syntactically, anyway. Stylistically? No particular standard is recommended at this time.
bbum
+2  A: 

Just to be really verbose :)

if (array == nil || array.count == 0)
willcodejavaforfood
+1  A: 

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.

Andy Riordan