I want to check if my array is empty or null, and on base of which I want to create a condition for example.
我想检查我的数组是否为空或空,在此基础上我想创建一个条件。
if(array == EMPTY){
//do something
}
I hope I'm clear what I am asking, just need to check if my array is empty?
我希望我讲得很清楚,只需要检查数组是否为空?
regards
问候
11 个解决方案
#1
100
if (!array || !array.count){
...
}
That checks if array is not nil, and if not - check if it is not empty.
它检查数组是否为nil,如果不是-检查它是否为空。
#2
25
if ([array count] == 0)
If the array is nil, it will be 0 as well, as nil maps to 0; therefore checking whether the array exists is unnecessary.
如果数组是nil,它也会是0,因为nil映射到0;因此,检查数组是否存在是不必要的。
Also, you shouldn't use array.count as some suggested. It may -work-, but it's not a property, and will drive anyone who reads your code nuts if they know the difference between a property and a method.
同样,不应该使用数组。算一些建议。它可以工作,但是它不是一个属性,如果任何读你的代码的人知道属性和方法之间的区别,他们会把你逼疯。
UPDATE: Yes, I'm aware that years later, count is now officially a property.
更新:是的,我知道数年后,count正式成为一项财产。
#3
10
you can try like this
你可以这样试试
if ([array count] == 0)
#4
7
Just to be really verbose :)
非常啰嗦:)
if (array == nil || array.count == 0)
#5
4
if (array == (id)[NSNull null] || [array count] == 0) {
NSLog(@"array is empty");
}
#6
4
Since Apple added the new firstObject property to NSArray, this also works.
由于苹果将新的firstObject属性添加到NSArray中,这也起了作用。
if (array.firstObject == nil)
{
// The array is empty
}
#7
2
As nil maps to 0, which equals NO
, the most elegant way should be
因为nil映射到0,等于NO,最优雅的方式应该是
if (![array count])
the '==' operator is not necessary.
不需要'= '操作符。
#8
1
You can also do this kind of test using if (nrow>0). If your data object is not formally an array, it may work better.
您还可以使用if (nrow>0)进行这种测试。如果您的数据对象不是正式的数组,那么它可能工作得更好。
#9
1
null and empty are not the same things , i suggest you treat them in differently
空和空不是一回事,我建议你用不同的方式对待它们。
if (array == [NSNull null]) {
NSLog(@"It's null");
} else if (array == nil || [array count] == 0) {
NSLog(@"It's empty");
}
#10
1
Swift 3
斯威夫特3
As in latest version of swift 3 the ability to compare optionals with > and < is not avaliable
在最新版本的swift 3中,与>和 <比较选项的能力无法保证< p>
It is still possible to compare optionals with ==, so the best way to check if an optional array contains values is:
仍然可以将选项与==进行比较,因此检查可选数组是否包含值的最佳方法是:
if array?.isEmpty == false {
print("There are objects!")
}
as per array count
按数组数
if array?.count ?? 0 > 0 {
print("There are objects!")
}
There are other ways also and can be checked here link to the answer
还有其他的方法可以在这里找到答案的链接
#11
0
if (array == nil || array.count == 0 || [array isEqaul [NSNull Null]])
#1
100
if (!array || !array.count){
...
}
That checks if array is not nil, and if not - check if it is not empty.
它检查数组是否为nil,如果不是-检查它是否为空。
#2
25
if ([array count] == 0)
If the array is nil, it will be 0 as well, as nil maps to 0; therefore checking whether the array exists is unnecessary.
如果数组是nil,它也会是0,因为nil映射到0;因此,检查数组是否存在是不必要的。
Also, you shouldn't use array.count as some suggested. It may -work-, but it's not a property, and will drive anyone who reads your code nuts if they know the difference between a property and a method.
同样,不应该使用数组。算一些建议。它可以工作,但是它不是一个属性,如果任何读你的代码的人知道属性和方法之间的区别,他们会把你逼疯。
UPDATE: Yes, I'm aware that years later, count is now officially a property.
更新:是的,我知道数年后,count正式成为一项财产。
#3
10
you can try like this
你可以这样试试
if ([array count] == 0)
#4
7
Just to be really verbose :)
非常啰嗦:)
if (array == nil || array.count == 0)
#5
4
if (array == (id)[NSNull null] || [array count] == 0) {
NSLog(@"array is empty");
}
#6
4
Since Apple added the new firstObject property to NSArray, this also works.
由于苹果将新的firstObject属性添加到NSArray中,这也起了作用。
if (array.firstObject == nil)
{
// The array is empty
}
#7
2
As nil maps to 0, which equals NO
, the most elegant way should be
因为nil映射到0,等于NO,最优雅的方式应该是
if (![array count])
the '==' operator is not necessary.
不需要'= '操作符。
#8
1
You can also do this kind of test using if (nrow>0). If your data object is not formally an array, it may work better.
您还可以使用if (nrow>0)进行这种测试。如果您的数据对象不是正式的数组,那么它可能工作得更好。
#9
1
null and empty are not the same things , i suggest you treat them in differently
空和空不是一回事,我建议你用不同的方式对待它们。
if (array == [NSNull null]) {
NSLog(@"It's null");
} else if (array == nil || [array count] == 0) {
NSLog(@"It's empty");
}
#10
1
Swift 3
斯威夫特3
As in latest version of swift 3 the ability to compare optionals with > and < is not avaliable
在最新版本的swift 3中,与>和 <比较选项的能力无法保证< p>
It is still possible to compare optionals with ==, so the best way to check if an optional array contains values is:
仍然可以将选项与==进行比较,因此检查可选数组是否包含值的最佳方法是:
if array?.isEmpty == false {
print("There are objects!")
}
as per array count
按数组数
if array?.count ?? 0 > 0 {
print("There are objects!")
}
There are other ways also and can be checked here link to the answer
还有其他的方法可以在这里找到答案的链接
#11
0
if (array == nil || array.count == 0 || [array isEqaul [NSNull Null]])