检查是否可选数组为空 [英] Check if optional array is empty
问题描述
在Objective-C中,当我有一个数组
In Objective-C, when I have an array
NSArray *array;
和我要检查,如果它不是空的,我总是这样:
and I want to check if it is not empty, I always do:
if (array.count > 0) {
NSLog(@"There are objects!");
} else {
NSLog(@"There are no objects...");
}
这样的话,就没有必要检查阵列==零
,因为这种情况会导致在code落入其他
情况下,还有一个非 - 无
,但空数组会做
That way, there is no need to check if array == nil
since this situation will lead the code to fall into the else
case, as well as a non-nil
but empty array would do.
然而,斯威夫特,我已经跨越中,我有一个可选的阵列的情况绊倒
However, in Swift, I have stumbled across the situation in which I have an optional array:
var array: [Int]?
和我不能够弄清楚要使用的条件。我有一些选择,如:
and I am not being able to figure out which condition to use. I have some options, like:
选项A:查看这两个非 - 无
,并在同等条件下空箱:
Option A: Check both non-nil
and empty cases in the same condition:
if array != nil && array!.count > 0 {
println("There are objects")
} else {
println("No objects")
}
选项B:取消绑定使用数组让
:
if let unbindArray = array {
if (unbindArray.count > 0) {
println("There are objects!")
} else {
println("There are no objects...")
}
} else {
println("There are no objects...")
}
选项C:使用合并运算符是斯威夫特规定:
Option C: Using the coalescing operator that Swift provides:
if (array?.count ?? 0) > 0 {
println("There are objects")
} else {
println("No objects")
}
我不喜欢这个选项的乙非常多,因为我是在重复code的两个条件。但我真的不知道有关的选项是否 A 和 C 是正确的,或者我应该使用这样做的任何其他方式。
I do not like the option B very much, because I am repeating code in two conditions. But I am not really sure about whether options A and C are correct or I should use any other way of doing this.
我知道,使用可选的阵列可以根据不同的情况是可以避免的,但在某些情况下,可能有必要问,如果它是空的。所以我想知道什么是做最简单的方式的方式。
I know that the use of an optional array could be avoided depending on the situation, but in some case it could be necessary to ask if it is empty. So I would like to know what is the way to do it the simplest way.
修改
由于@vacawama指出,检查它的这种简单的方式工作:
As @vacawama pointed out, this simple way of checking it works:
if array?.count > 0 {
println("There are objects")
} else {
println("No objects")
}
不过,我是想在我想要做一些特别的东西,只有当它是无
或空的情况下,再继续无论数组是否有元素或不。所以我尝试:
However, I was trying the case in which I want to do something special only when it is nil
or empty, and then continue regardless whether the array has elements or not. So I tried:
if array?.count == 0 {
println("There are no objects")
}
// Do something regardless whether the array has elements or not.
和也
if array?.isEmpty == true {
println("There are no objects")
}
// Do something regardless whether the array has elements or not.
但是,当阵列无
,它不会落入如果
体。这是因为,在这种情况下,数组?.Count之间的==零
和数组?.isEmpty ==零
,所以前pressions 数组?.Count之间的== 0
和数组?.isEmpty ==真
这两个评估为假
。
But, when array is nil
, it does not fall into the if
body. And this is because, in that case, array?.count == nil
and array?.isEmpty == nil
, so the expressions array?.count == 0
and array?.isEmpty == true
both evaluate to false
.
所以我试图找出是否有只有一个条件,以及实现这一目标的任何方式。
So I am trying to figure out if there is any way of achieve this with just one condition as well.
推荐答案
您可以简单地做:
if array?.count > 0 {
print("There are objects")
} else {
print("No objects")
}
作为@马丁在评论中指出,它使用 FUNC>< T:_Comparable>(LHS:T 1,RHS:ΔT) - >布尔
这意味着编译器包装 0
为诠释?
,这样的比较可以与左手边是一个诠释?
,因为可选的链接调用。
As @Martin points out in the comments, it uses func ><T : _Comparable>(lhs: T?, rhs: T?) -> Bool
which means that the compiler wraps 0
as an Int?
so that the comparison can be made with the left hand side which is an Int?
because of the optional chaining call.
在以类似的方式,你可以这样做:
In a similar way, you could do:
if array?.isEmpty == false {
print("There are objects")
} else {
print("No objects")
}
请注意:你必须明确地用比较假
这里这个工作
Note: You have to explicitly compare with false
here for this to work.
如果你想要做的事,当阵列无
或为空,至少有7个选项:
If you want to do something when the array is nil
or is empty, you have at least 7 choices:
选项A:
if !(array?.count > 0) {
print("There are no objects")
}
选项B:
if !(array?.isEmpty == false) {
print("There are no objects")
}
选项C:
if array == nil || array!.count == 0 {
print("There are no objects")
}
选项D:
if array == nil || array!.isEmpty {
print("There are no objects")
}
选项E:
if (array ?? []).isEmpty {
print("There are no objects")
}
选项F:
if array?.isEmpty ?? true {
print("There are no objects")
}
选项G:
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
选项D 准确地描绘了如何用英文描述的那样:我想要做一些特别的东西,只有当它是零或空我会建议你使用这一点,因为这是很容易理解。没有什么错,特别是因为它会短路,并跳过检查空变量是否无
。
Option D exactly captures how you described it in English: "I want to do something special only when it is nil or empty." I would recommend that you use this since it is easy to understand. There is nothing wrong with this, especially since it will "short circuit" and skip the check for empty if the variable is nil
.
这篇关于检查是否可选数组为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!