开发者社区> 问答> 正文

检查可选数组是否为空

在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:

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,因为我会在两种情况下重复执行代码。但是我不确定选项A和C是否正确,还是应该使用其他任何方式来做到这一点。

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

展开
收起
保持可爱mmm 2020-02-09 12:07:26 527 0
1 条回答
写回答
取消 提交回答
  • 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或为空时做一些特别的事情。” 我建议您使用它,因为它很容易理解。这没有什么错,特别是因为如果变量为,它将“短路”并跳过对空的检查nil。

    Swift 2.x的先前答案: 您可以简单地执行以下操作:

    if array?.count > 0 { print("There are objects") } else { print("No objects") } 正如@Martin在注释中指出的那样,它使用func > (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或为空时做一些特别的事情。” 我建议您使用它,因为它很容易理解。这没有什么错,特别是因为如果变量为,它将“短路”并跳过对空的检查nil。

    分享改善这个答案 19年5月15日在14:24编辑 14年12月21日在11:39 回答

    瓦瓦瓦玛 114k2222金币208208银章227个227青铜徽章 2

    2020-02-09 12:07:44
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载