如何检查NSArray在iOS中为空或空?
在NSArray被分配和初始化后,如果没有添加到NSArray中,如何检查它是空的还是空的?
谢谢。
if (array == nil || [array count] == 0) { ... } NSArray的计数方法,一个常见的方法来做到这一点…
 if (![self.myArray count]) { } 
这将检查数组是否没有任何内容,或者是否设置为零。
虽然我们都抛出了相同的答案,我还以为我也会。
 if ([array count] < 1) { ... } 
和另一个
 if(!array || array.count==0) 
  if([myarray count])它会检查不是空和无数组。 
试试这个
 if(array == [NSNull null] || [array count] == 0) { } 
你可以使用这个:
 if (!anArray || [anArray count] == 0) { /* Your code goes here */ } 
使用
 (array.count ? array : nil) 
 如果array = nil以及[array count] == 0 ,它将返回nil 
 if([arrayName count]==0) { //array is empty. } else { //array contains some elements. } 
 if (array == nil && [array count] == 0) { ... } 
我使用这个代码,因为当我的pickerview数组是空的时候遇到了麻烦
我的代码是
 - (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]; }