检查是否可选数组为空 [英] Check if optional array is empty

查看:191
本文介绍了检查是否可选数组为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆