58

After a NSArray was alloc and init, if there is nothing added to the NSArray, how to check it is null or empty ?

Thanks.

10 Answers 10

113
if (array == nil || [array count] == 0) {
    ...
}
Sign up to request clarification or add additional context in comments.

8 Comments

You should use nil when dealing with objects (see stackoverflow.com/questions/557582/null-vs-nil-in-objective-c).
Should you do both? Or will [array count] == 0 return TRUE if the array is nil?
No you don't need both. Sending a message through a nil object is ignored in Objective-C and will yield 0. Therefore if ([array count] == 0) { ... } is enough.
@trojanfoe sending a message to nil is NOT ignored, but rather has strict predefined behavior. In this case 'count' message is supposed to return an NSInteger - so it does exactly that. returns zero (0).
@MottiShneor Sending a message to a nil is effectively ignored then. OK? It won't crash like pretty much every other language.
|
24

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.

2 Comments

if myArray is nil, will it crash due to Null Pointer Exception?
@OMGPOP: No, it doesn't
14

While we are all throwing out the same answers, I thought I would too.

if ([array count] < 1) {
    ...
}

3 Comments

+1 for anonymous downvote to a response which is (apart from some people's personal preferences) as good and correct as the others. the reason this works, is because the objc message will return 0 in this case if array is nil -- it's well defined.
thanks for the upvote. Didn't really understand why i deserved a downvote apart from the witty comment which should be ignored as the only thing that matters is the code.
Why < 1 and not == 0? < 1 indicates that we can also get negative numbers and this doesn't seem to be the case.
9

and another

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

2 Comments

that would mean that the array was not nil. but it still could be empty, hence logical or to check to see if the count is 0. If the array is nil (!array), the second part won't even be evaluated.
array.count will be 0 if array is nil. This is well defined Objective-C behavior. Excessive nil checking in Objective-C is something is primarily carried over by Java/C# developers.
6

if([myarray count]) It checks for both not empty and nil array.

Comments

4

Try this one

if(array == [NSNull null] || [array count] == 0) {
}

3 Comments

What is the difference between (array == [NSNull null]) and (array == nil) ?
@user403015 [NSNull null] is a object of type NSNull. Thus if array == [NSNull null], then array is a pointer to that NSNull singleton object. [NSNull null is frequently used when you want to store an object (e.g. in a dictionary or array), where nil would not be accepted. If array == nil then that means that array is zeroed out (points to 0x0).
@OhhMee I don't know who downvoted it, but it's probably because array == [NSNull null] is not a very useful check, because, for example, if the alloc and init failed, it would be nil, not [NSNull null]. Given the scenario that user403015 outlined, [NSNull null] is not a scenario that could arise.
4
if([arrayName count]==0)
{
    //array is empty.
}
else
{
   //array contains some elements.
}

Comments

3

You can use this :

if (!anArray || [anArray count] == 0) {
    /* Your code goes here */
}

2 Comments

@Joel Please explain the reason for downvoting.
while it is legit to ask about why you have been downvoted (I'd do it as well), you should not jump to conclusions. I'm pretty sure I did not vote your answer (neither up nor down). The only thing I've done was suggest an edit (which has been approved, I see).
3

use

(array.count ? array : nil)

It will return nil if array = nil as well as [array count] == 0

Comments

1
if (array == nil && [array count] == 0) {
...
}

I use this code because I am having trouble to my pickerview when its the array is empty

My code is

- (IBAction)btnSelect:(UIBarButtonItem *)sender { // 52
if (self.array != nil && [self.array count] != 0) {
    NSString *select = [self.array objectAtIndex:[self.pickerView selectedRowInComponent:0]];

    if ([self.pickListNumber isEqualToString:@"1"]) {
        self.textFieldCategory.text = select;
        self.textFieldSubCategory.text = @"";
    } else if ([self.pickListNumber isEqualToString:@"2"]) {
        self.textFieldSubCategory.text = select;
    }

    [self matchSubCategory:select];
} else {
    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                          message:@"You should pick Category first"
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles: nil];
    [myAlertView show];
}

[self hidePickerViewContainer:self.viewCategory];
}

2 Comments

The code in the first line and the code inside the example code is different.
The first block has an inherent bug. If (array == nil) how can it ever have a 'count' ??? this if is meaningless, and will always return false.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.