Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if optional array is empty

Tags:

arrays

ios

swift

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.

like image 630
eze.scaruli Avatar asked Dec 21 '14 11:12

eze.scaruli


People also ask

How do you check the array is empty or not?

To check if an array is empty or not, you can use the . length property. The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not.

How check if array is empty Swift?

To check if an array is empty, use Swift function isEmpty on the array. Following is a quick example, to check if an array is empty. The function returns a boolean value. If the array is empty, isEmpty returns true, else false.

How do you check if an array is empty in Objective C?

It is still possible to compare optionals with ==, so the best way to check if an optional array contains values is: if array?. isEmpty == false { print("There are objects!") }

Is empty array Nullish?

To check if an array is not empty we simply test if the length property is not 0 . 0 , null , undefined are falsy values, which means they are evaluated to false in a boolean context like the condition for the if statement. Here is another way to check if an array is not empty.


2 Answers

Updated answer for Swift 3 and above:

Swift 3 has removed the ability to compare optionals with > and <, so some parts of the previous answer are no longer valid.

It is still possible to compare optionals with ==, so the most straightforward way to check if an optional array contains values is:

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

Other ways it can be done:

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

If you want to do something when the array is nil or is empty, you have at least 6 choices:

Option A:

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

Option B:

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

Option C:

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

Option D:

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

Option E:

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

Option F:

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

Option C exactly captures how you described it in English: "I want to do something special only when it is nil or empty." I would recommend that you use this since it is easy to understand. There is nothing wrong with this, especially since it will "short circuit" and skip the check for empty if the variable is nil.



Previous answer for Swift 2.x:

You can simply do:

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

As @Martin points out in the comments, it uses func ><T : _Comparable>(lhs: T?, rhs: T?) -> Bool which means that the compiler wraps 0 as an Int? so that the comparison can be made with the left hand side which is an Int? because of the optional chaining call.

In a similar way, you could do:

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

Note: You have to explicitly compare with false here for this to work.


If you want to do something when the array is nil or is empty, you have at least 7 choices:

Option A:

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

Option B:

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

Option C:

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

Option D:

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

Option E:

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

Option F:

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

Option G:

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

Option D exactly captures how you described it in English: "I want to do something special only when it is nil or empty." I would recommend that you use this since it is easy to understand. There is nothing wrong with this, especially since it will "short circuit" and skip the check for empty if the variable is nil.

like image 73
vacawama Avatar answered Oct 21 '22 23:10

vacawama


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")
}
like image 8
Mobile Dan Avatar answered Oct 21 '22 23:10

Mobile Dan