如何在 iOS 中检查 NSArray 为 null 或为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5932528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:50:30  来源:igfitidea点击:

How to check NSArray is null or empty in iOS?

iphoneiosipad

提问by user403015

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

一个 NSArray 被分配和初始化后,如果没有添加到 NSArray 中,如何检查它是 null 还是空?

Thanks.

谢谢。

回答by Chris Doble

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

回答by Joshua Weinberg

NSArray has the count method, a common way to do it would be...

NSArray 有 count 方法,一种常用的方法是......

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

That will check if the array has nothing in it, or if it is set to nil.

这将检查数组中是否没有任何内容,或者它是否设置为 nil。

回答by shabbirv

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

虽然我们都抛出相同的答案,但我想我也会。

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

回答by Jason Cragun

and another

而另一个

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

回答by chaithraVeeresh

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

if([myarray count])它检查非空和 nil 数组。

回答by infiniteLoop

use

(array.count ? array : nil)

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

如果array = nil以及,它将返回 nil[array count] == 0

回答by soorej babu

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

回答by PgmFreek

Try this one

试试这个

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

回答by RandomGuy

You can use this :

你可以使用这个:

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

回答by user313879

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