Ios 检查可选数组是否为空
在Objective-C中,当我有一个数组时Ios 检查可选数组是否为空,ios,arrays,swift,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中,我偶然发现我有一个可选
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是否正确,或者我应该使用其他方法
我知道根据情况可以避免使用可选数组,但在某些情况下,可能需要询问它是否为空。所以我想知道最简单的方法是什么
编辑:
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if !(array?.count > 0) {
print("There are no objects")
}
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if array.isNilOrEmpty {
print("The array is nil or empty")
}
正如@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==nil
和array?.isEmpty==nil
,因此表达式array?.count==0
和array?.isEmpty==true
的计算结果都是false
因此,我试图找出是否有任何方法可以通过一个条件实现这一点。更新了Swift 3及以上版本的答案:
Swift 3已经取消了将选项与
和(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?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if !(array?.count > 0) {
print("There are no objects")
}
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if array.isNilOrEmpty {
print("The array is nil or empty")
}
选项B:
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if !(array?.count > 0) {
print("There are no objects")
}
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if array.isNilOrEmpty {
print("The array is nil or empty")
}
选项C:
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if !(array?.count > 0) {
print("There are no objects")
}
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if array.isNilOrEmpty {
print("The array is nil or empty")
}
选项D:
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if !(array?.count > 0) {
print("There are no objects")
}
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if array.isNilOrEmpty {
print("The array is nil or empty")
}
选项E:
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if !(array?.count > 0) {
print("There are no objects")
}
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if array.isNilOrEmpty {
print("The array is nil or empty")
}
选项F:
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if !(array?.count > 0) {
print("There are no objects")
}
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if array.isNilOrEmpty {
print("The array is nil or empty")
}
选项G:
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if !(array?.count > 0) {
print("There are no objects")
}
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if array.isNilOrEmpty {
print("The array is nil or empty")
}
选项D准确地捕捉了您用英语描述它的方式:只有当它为零或为空时,我才想做一些特别的事情。我建议您使用它,因为它很容易理解。这并没有什么问题,特别是当变量为
nil
条件展开时,它会“短路”并跳过空检查:
if let anArray = array {
if !anArray.isEmpty {
//do something
}
}
编辑:可能从Swift 1.2开始:
if let myArray = array where !myArray.isEmpty {
// do something with non empty 'myArray'
}
编辑:可能自Swift 2.0起:
guard let myArray = array where !myArray.isEmpty else {
return
}
// do something with non empty 'myArray'
选项D:如果数组不需要是可选的,因为您只关心它是否为空,请将其初始化为空数组,而不是可选数组:
var array = [Int]()
现在它将一直存在,您只需检查isEmpty
集合
协议上的扩展属性
*用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?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if !(array?.count > 0) {
print("There are no objects")
}
if !(array?.isEmpty == false) {
print("There are no objects")
}
if array == nil || array!.count == 0 {
print("There are no objects")
}
if array == nil || array!.isEmpty {
print("There are no objects")
}
if (array ?? []).isEmpty {
print("There are no objects")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
if array.isNilOrEmpty {
print("The array is nil or empty")
}
其他选择
除了上面的扩展,我发现以下选项在没有强制展开选项的情况下最清晰。我将此理解为展开可选数组,如果为nil,则替换相同类型的空数组。然后,获取(非可选)结果,如果为空,则执行条件代码
推荐
if (array ?? []).isEmpty {
print("The array is nil or empty")
}
虽然下面的内容读得很清楚,但我建议尽量避免强制展开选项。虽然您可以保证array
在array!时永远不会nil
!。如果在这种特定情况下执行isEmpty
,则以后很容易对其进行编辑,并在无意中导致崩溃。当您习惯使用强制展开选项时,您增加了将来有人进行更改的可能性,该更改会编译但在运行时崩溃
不推荐强>
if array == nil || array!.isEmpty {
print("The array is nil or empty")
}
我发现包含数组?
(可选链接)的选项令人困惑,例如:
令人困惑?
if !(array?.isEmpty == false) {
print("The array is nil or empty")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
Swift 3-4兼容:
extension Optional where Wrapped: Collection {
var nilIfEmpty: Optional {
switch self {
case .some(let collection):
return collection.isEmpty ? nil : collection
default:
return nil
}
}
var isNilOrEmpty: Bool {
switch self {
case .some(let collection):
return collection.isEmpty
case .none:
return true
}
}
用法:
guard let array = myObject?.array.nilIfEmpty else { return }
或
优雅的内置解决方案是可选的map
方法。这种方法经常被遗忘,但它正是您在这里所需要的;它允许你发送一条信息给包裹在一个可选的、安全的文件中的东西。在这种情况下,我们使用了一种三向开关:我们可以对可选数组说isEmpty
,然后得到true、false或nil(如果数组本身为nil)
与其使用if
和else
检查空数组,不如使用guard
检查空数组,而不为同一数组创建新变量
guard !array.isEmpty else {
return
}
// do something with non empty ‘array’
最好使用if检查空数组。为什么是bcoz,例如,如果我们试图在列表数组中查找最大整数,显然我们会将列表[0]与列表中的每个整数进行比较。当时它给了我们索引超出范围的异常。。。。您可以同时使用IF和Guard进行尝试
func findGreatestInList(list: [Int]?)-> Int? {
if list!.count == 0 {
print("List is empty")
return nil
}
/*guard list!.isEmpty else {
print("List is empty")
return nil
}*/
var greatestValue = list![0]
for number in 0...list!.count-1 {
if list![number] > greatestValue {
greatestValue = list![number]
他还要检查它是否是空的。这只是检查可选的是否包含数组。是的,然后您可以执行if array?!=零&!(array!.isEmpty){}
您可以简单地将其展开,因为如果arr