2
I need to know if a Nsarray is empty or void using an if.
Code:
NSArray* array;
if( array == nil)
{
// Fazer algo
}
2
I need to know if a Nsarray is empty or void using an if.
Code:
NSArray* array;
if( array == nil)
{
// Fazer algo
}
4
As in objective-c a message to nil
returns nil
, you can use the code below:
NSArray *array = [obtem o valor];
if ([array count]) {
// Nao é vazio nem nulo
}
If the array is nil
, then the result of [array count]
will also be nil
(what is considered "false" in the if). If it is not nil
, then the method count
returns the number of elements. If it is zero, if will not enter either, since zero is also considered false.
1
Well, it was quicker than I thought.
I used NULL instead of Nil, staying this way:
NSArray* array;
if( array == NULL)
{
// Fazer algo
}
Browser other questions tagged array objective-c if syntax
You are not signed in. Login or sign up in order to post.
I found another solution... using NULL instead of nil!
– Tiago Amaral
Thanks @carlosfigueira, your answer is very good!!
– Tiago Amaral
To clarify better why use the
([array count])
in place ofNULL
has a very cool article at this Nshipster link– iTSangar
Great link!! Thank you! @iTsangar
– Tiago Amaral