ios - 检查可选数组是否为空

标签 ios arrays swift

在 Objective-C 中,当我有一个数组时

NSArray *array;

我想检查它是否不为空,我总是这样做:

if (array.count > 0) {
    NSLog(@"There are objects!");
} else {
    NSLog(@"There are no objects...");
}

这样,就不需要检查 array == nil 因为这种情况会导致代码落入 else 的情况,以及非-nil 但空数组也可以。

但是,在 Swift 中,我偶然发现了我有一个可选数组的情况:

var array: [Int]?

而且我不知道要使用哪个条件。我有一些选择,例如:

选项 A:在相同条件下检查非 nil 和空情况:

if array != nil && array!.count > 0 {
    println("There are objects")
} else {
    println("No objects")
}

选项 B: 使用 let 取消绑定(bind)数组:

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:使用 Swift 提供的合并运算符:

if (array?.count ?? 0) > 0 {
    println("There are objects")
} else {
    println("No objects")
}

我不太喜欢选项B,因为我在两种情况下重复代码。但我不确定选项 AC 是否正确,或者我应该使用任何其他方式来执行此操作。

我知道根据情况可以避免使用可选数组,但在某些情况下可能需要询问它是否为空。所以我想知道最简单的方法是什么。


编辑:

正如@vacawama 所指出的,这种简单的检查方法有效:

if array?.count > 0 {
    println("There are objects")
} else {
    println("No objects")
}

然而,我正在尝试这样一种情况,即我只想在它为 nil 或为空时做一些特殊的事情,然后不管数组是否有元素都继续。所以我尝试了:

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.

但是,当数组为nil 时,它不会落入if 主体。这是因为,在那种情况下,array?.count == nilarray?.isEmpty == nil,所以表达式 array?.count = = 0array?.isEmpty == true 的计算结果均为 false

所以我想弄清楚是否有任何方法可以仅在一个条件下实现这一目标。

最佳答案

针对 Swift 3 及更高版本的更新答案:

Swift 3 移除了将可选值与 > 进行比较的能力和 < ,因此之前答案的某些部分不再有效。

仍然可以将可选值与 == 进行比较, 因此检查可选数组是否包含值的最直接方法是:

if array?.isEmpty == false {
    print("There are objects!")
}

其他方法:

if array?.count ?? 0 > 0 {
    print("There are objects!")
}

if !(array?.isEmpty ?? true) {
    print("There are objects!")
}

if array != nil && !array!.isEmpty {
    print("There are objects!")
}

if array != nil && array!.count > 0 {
    print("There are objects!")
}

if !(array ?? []).isEmpty {
    print("There are objects!")
}

if (array ?? []).count > 0 {
    print("There are objects!")
}

if let array = array, array.count > 0 {
    print("There are objects!")
}

if let array = array, !array.isEmpty {
    print("There are objects!")
}

如果你想在数组为nil时做点什么或者是空的,你至少有 6 个选择:

选项 A:

if !(array?.isEmpty == false) {
    print("There are no objects")
}

选项 B:

if array == nil || array!.count == 0 {
    print("There are no objects")
}

选项 C:

if array == nil || array!.isEmpty {
    print("There are no objects")
}

选项 D:

if (array ?? []).isEmpty {
    print("There are no objects")
}

选项 E:

if array?.isEmpty ?? true {
    print("There are no objects")
}

选项 F:

if (array?.count ?? 0) == 0 {
    print("There are no objects")
}

选项 C 完全符合您用英语描述它的方式:“我只想在它为零或为空时做一些特别的事情。”我建议您使用这是因为它很容易理解。这没有任何问题,特别是因为如果变量为 nil,它将“短路”并跳过空检查。 .



Swift 2.x 的先前答案:

你可以简单地做:

if array?.count > 0 {
    print("There are objects")
} else {
    print("No objects")
}

正如@Martin 在评论中指出的那样,它使用 func ><T : _Comparable>(lhs: T?, rhs: T?) -> Bool这意味着编译器包装了 0作为Int?这样可以与左侧进行比较,即 Int?因为可选的链接调用。

以类似的方式,你可以这样做:

if array?.isEmpty == false {
    print("There are objects")
} else {
    print("No objects")
}

注意:您必须明确地与 false 进行比较在这里工作。


如果你想在数组为nil时做点什么或者是空的,你至少有 7 个选择:

选项 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 完全符合您用英语对其的描述:“我只想在它为零或为空时做一些特别的事情。”我建议您使用这是因为它很容易理解。这没有任何问题,特别是因为如果变量为 nil,它将“短路”并跳过空检查。 .

关于ios - 检查可选数组是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27588964/

相关文章:

iphone - NSURLRequest 发布到 HTTPS URL 安全吗?

android - whatsApp 发送消息让用户在 ios 中编辑消息,在 android 中它没有

arrays - 在一行和 1's and 0' s 的矩阵之间进行 xor 运算的更快方法?

uitableview - 如何将 UILabels 正确插入到每个 tebleview 单元格中

ios - 导航 Controller 的问题(后退按钮和prepareForSegue)

swift - 为什么要通过向量的 sqrt 创建单位向量

iOS xcode 麦克风音量检测?

iPhone 。自定义导航 Controller

python - numpy.arrays 的 fsum,稳定求和

php - 在 TWIG 中渲染多维数组