How to check that NSArray is null or empty in iOS?

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

Thank.

+47
ios iphone ipad
May 9 '11 at 4:31
source share
10 answers
if (array == nil || [array count] == 0) { ... } 
+102
May 9 '11 at 4:33
source share

NSArray has a counting method, a general way to do this would be ...

 if (![self.myArray count]) { } 

This will check to see if it has an array or is set to nil.

+23
May 9 '11 at 4:33
source share

As we all throw out the same answers, I thought so too.

 if ([array count] < 1) { ... } 
+12
May 9 '11 at 4:37
source share

and further

 if(!array || array.count==0) 
+9
May 9 '11 at 4:34
source share

if([myarray count]) It checks both a non-empty and a null array.

+5
Apr 24 '14 at 10:37
source share

Try this one

 if(array == [NSNull null] || [array count] == 0) { } 
+3
May 9 '11 at 4:48
source share

You can use this:

 if (!anArray || [anArray count] == 0) { /* Your code goes here */ } 
+3
Feb 14 '14 at 11:18
source share

using

 (array.count ? array : nil) 

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

+3
Apr 16 '15 at 5:45
source share
 if([arrayName count]==0) { //array is empty. } else { //array contains some elements. } 
+3
Apr 13 '16 at 10:16
source share
 if (array == nil && [array count] == 0) { ... } 

I use this code because I have problems with my pickerview when its array is empty.

My code

 - (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
Mar 05 '15 at 18:35
source share



All Articles