How to check NSArray is null or empty in iOS?

IphoneIosIpad

Iphone Problem Overview


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

Thanks.

Iphone Solutions


Solution 1 - Iphone

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

Solution 2 - Iphone

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.

Solution 3 - Iphone

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

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

Solution 4 - Iphone

and another

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

Solution 5 - Iphone

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

Solution 6 - Iphone

Try this one

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

Solution 7 - Iphone

if([arrayName count]==0)
{
    //array is empty.
}
else
{
   //array contains some elements.
}

Solution 8 - Iphone

You can use this :

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

Solution 9 - Iphone

use

(array.count ? array : nil)

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

Solution 10 - Iphone

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];
}

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
Questionuser403015View Question on Stackoverflow
Solution 1 - Iphoneuser94896View Answer on Stackoverflow
Solution 2 - IphoneJoshua WeinbergView Answer on Stackoverflow
Solution 3 - IphoneshabbirvView Answer on Stackoverflow
Solution 4 - IphoneJason CragunView Answer on Stackoverflow
Solution 5 - IphonechaithraVeereshView Answer on Stackoverflow
Solution 6 - IphonePgmFreekView Answer on Stackoverflow
Solution 7 - IphoneSoorej BabuView Answer on Stackoverflow
Solution 8 - IphoneRandomGuyView Answer on Stackoverflow
Solution 9 - IphoneinfiniteLoopView Answer on Stackoverflow
Solution 10 - Iphoneuser313879View Answer on Stackoverflow