xcode Obj-C:检查空数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6863087/
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-09-14 21:35:50  来源:igfitidea点击:

Obj-C: Check for Empty Array

iphoneobjective-cxcode

提问by Baub

How do you check if an array is empty? (for the record, I looked at similar questions but didn't find the one with this exact problem).

如何检查数组是否为空?(作为记录,我查看了类似的问题,但没有找到有这个确切问题的问题)。

I have a NSMutableArray(let's call it nsma)that I need to check if empty. If I do NSLog(@"nsma: %@",nsma);it logs nsma: ( ), but if I do NSLog(@"nsma count:%@",nsma);it logs nsma: (null). I need to check if it is empty, but my if statement that does that isn't working for some reason:

我有一个NSMutableArray(我们称之为nsma)我需要检查是否为空。如果我这样做NSLog(@"nsma: %@",nsma);会记录nsma: ( ),但如果我这样做NSLog(@"nsma count:%@",nsma);会记录nsma: (null)。我需要检查它是否为空,但我的 if 语句由于某种原因不起作用:

if (nsma == nil)
{
    NSLog(@"nsma is empty");
}

Does anyone know what is going on?

有谁知道发生了什么?

Thanks for the help in advance.

我在这里先向您的帮助表示感谢。

回答by InsertWittyName

if([nsma count] == 0)
{
    NSLog(@"nsma is empty");
}

回答by Aravindhan

check like this

像这样检查

    if([array count]==0)
    {
    NSLog(@"Empty");
    }
    else
    {
    NSLog(@"not Empty");
    }