检查可选数组是否为空
在Objective-C中,当我有一个数组
NSArray *array; 我想检查一下是不是空的,我总是这样做:
 if (array.count > 0) { NSLog(@"There are objects!"); } else { NSLog(@"There are no objects..."); } 
 这样,没有必要检查array == nil因为这种情况会导致代码陷入else情况,以及一个非零,但空的数组会做。 
然而,在Swift中,我偶然发现了我有一个可选数组的情况:
 var array: [Int]? 
我无法弄清楚使用哪种条件。 我有一些select,如:
选项A:在相同的条件下检查非空和空的情况:
 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是否正确,或者我应该使用其他方式来执行此操作。
我知道可以根据情况避免使用可选的数组,但在某些情况下可能需要询问是否为空。 所以我想知道怎么做最简单的方法。
编辑:
就像@vacawama指出的那样,检查它的简单方法是:
 if array?.count > 0 { println("There are objects") } else { println("No objects") } 
但是,我正在尝试的情况下,我只想做一些特殊的事情,只有当它是空或空,然后继续不pipe数组是否有元素。 所以我试了一下:
 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 == nil和array?.isEmpty == nil ,所以expression式array?.count == 0和array?.isEmpty == true都计算为false 。 
所以我试图找出是否有任何方式来实现这一点,只有一个条件。
更新了Swift 3的答案:
  Swift 3删除了将选项与>和<进行比较的function,因此前一个答案的某些部分不再有效。 
 仍然可以将可选项与==进行比较,因此检查可选数组是否包含值的最直接的方法是: 
 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或空的时候做一些事情,你至less有6个select: 
选项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正好描述了你如何用英语描述它: “我只想在零或空的时候做一些特殊的事情。 我会build议你使用这个,因为它很容易理解。 这没有什么不妥,尤其是因为它会“短路”,如果variablesnil ,跳过空检查。 
先前的答案为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?的左边进行比较了Int? 因为可选的链接调用。 
以类似的方式,你可以这样做:
 if array?.isEmpty == false { print("There are objects") } else { print("No objects") } 
注意:你必须在这里明确地比较这个工作。
 如果你想在数组nil或空的时候做一些事情,你至less有7个select: 
选项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正好说明了你如何用英语描述它: “我只想在零或空的时候做一些特殊的事情。 我会build议你使用这个,因为它很容易理解。 这没有什么不妥,尤其是因为它会“短路”,如果variablesnil ,跳过空检查。 
选项D:如果数组不需要是可选的,因为只关心它是否为空,请将其初始化为空数组而不是可选:
 var array = [Int]() 
 现在它一直存在,你可以简单地检查isEmpty 。 
有条件解包:
 if let anArray = array { if !anArray.isEmpty { //do something } } 
编辑:可能,因为斯威夫特1.2:
 if let myArray = array where !myArray.isEmpty { // do something with non empty 'myArray' } 
编辑:可能,因为斯威夫特2.0:
 guard let myArray = array where !myArray.isEmpty else { return } // do something with non empty 'myArray' 
  Collection协议的扩展属性 
*用Swift写成3
 extension Optional where Wrapped: Collection { var isNilOrEmpty: Bool { switch self { case .some(let collection): return collection.isEmpty case .none: return true } } } 
使用示例:
 if array.isNilOrEmpty { print("The array is nil or empty") } 
其他选项
 除了上面的扩展名外,我发现下面的选项最清楚,没有强制解包选项。 我读这个解开可选数组,如果nil,用相同types的空数组replace。 然后,取这个(非可选的)结果,如果它是isEmpty执行条件代码。 
推荐的
 if (array ?? []).isEmpty { print("The array is nil or empty") } 
 虽然以下内容清楚,但我build议尽可能避免强行解开可选项的习惯。 尽pipe在这种情况下,当array!.isEmpty被执行时,你可以保证array永远不会nil ,但是稍后编辑它会很容易,并且会不经意间引入崩溃。 当你变得舒适的解包可选项时,你会增加某人在将来编译但在运行时崩溃的机会。 
不build议!
 if array == nil || array!.isEmpty { print("The array is nil or empty") } 
 我find包含array?选项array?  (可选链)混淆如: 
混乱?
 if !(array?.isEmpty == false) { print("The array is nil or empty") } if array?.isEmpty ?? true { print("There are no objects") }