Check if optional array is empty

2020-01-27 00:16发布

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...");
}

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:

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")
}

Option B: Unbind the array using 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...")
}

Option C: Using the coalescing operator that Swift provides:

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

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.


EDIT:

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.

And also

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

// Do something regardless whether the array has elements or not.

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.

标签: ios arrays swift
7条回答
Emotional °昔
2楼-- · 2020-01-27 00:45

Instead of using if and else it is better way just to use guard to check for empty array without creating new variables for the same array.

guard !array.isEmpty else {
    return
}
// do something with non empty ‘array’
查看更多
贼婆χ
3楼-- · 2020-01-27 00:47

The elegant built-in solution is Optional's map method. This method is often forgotten, but it does exactly what you need here; it allows you to send a message to the thing wrapped inside an Optional, safely. We end up in this case with a kind of threeway switch: we can say isEmpty to the Optional array, and get true, false, or nil (in case the array is itself nil).

var array : [Int]?
array.map {$0.isEmpty} // nil (because `array` is nil)
array = []
array.map {$0.isEmpty} // true (wrapped in an Optional)
array?.append(1)
array.map {$0.isEmpty} // false (wrapped in an Optional)
查看更多
走好不送
4楼-- · 2020-01-27 00:49

Extension Property on the Collection Protocol

*Written in Swift 3

extension Optional where Wrapped: Collection {
    var isNilOrEmpty: Bool {
        switch self {
            case .some(let collection):
                return collection.isEmpty
            case .none:
                return true
        }
    }
}

Example Use:

if array.isNilOrEmpty {
    print("The array is nil or empty")
}

 

Other Options

Other than the extension above, I find the following option most clear without force unwrapping optionals. I read this as unwrapping the optional array and if nil, substituting an empty array of the same type. Then, taking the (non-optional) result of that and if it isEmpty execute the conditional code.

Recommended

if (array ?? []).isEmpty {
    print("The array is nil or empty")
}

Though the following reads clearly, I suggest a habit of avoiding force unwrapping optionals whenever possible. Though you are guaranteed that array will never be nil when array!.isEmpty is executed in this specific case, it would be easy to edit it later and inadvertently introduce a crash. When you become comfortable force unwrapping optionals, you increase the chance that someone will make a change in the future that compiles but crashes at runtime.

Not Recommended!

if array == nil || array!.isEmpty {
    print("The array is nil or empty")
}

I find options that include array? (optional chaining) confusing such as:

Confusing?

if !(array?.isEmpty == false) {
    print("The array is nil or empty")
}

if array?.isEmpty ?? true {
    print("There are no objects")
}
查看更多
▲ chillily
5楼-- · 2020-01-27 00:50

Option D: If the array doesn't need to be optional, because you only really care if it's empty or not, initialise it as an empty array instead of an optional:

var array = [Int]()

Now it will always exist, and you can simply check for isEmpty.

查看更多
仙女界的扛把子
6楼-- · 2020-01-27 00:51

Conditional unwrapping:

if let anArray = array {
    if !anArray.isEmpty {
        //do something
    }
}

EDIT: Possible since Swift 1.2:

if let myArray = array where !myArray.isEmpty {
    // do something with non empty 'myArray'
}

EDIT: Possible since Swift 2.0:

guard let myArray = array where !myArray.isEmpty else {
    return
}
// do something with non empty 'myArray'
查看更多
\"骚年 ilove
7楼-- · 2020-01-27 00:52

Swift 3-4 compatible:

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
        }
}

Usage:

guard let array = myObject?.array.nilIfEmpty else { return }

Or

if myObject.array.isNilOrEmpty {
    // Do stuff here
}
查看更多
登录 后发表回答