Updated answer for Swift 3 and above:
Swift 3 removed the ability to compare options with > and < , so some parts of the previous answer are no longer valid.
It is still possible to compare optional values with == , so the easiest way to check if an optional array contains values is:
if array?.isEmpty == false { print("There are objects!") }
Other ways to do this:
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 empty, you have at least 6 options:
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 accurately reflects how you described it in English: "I want to do something special only when it's empty or zero." I would recommend you use this as it is easy to understand. There is nothing wrong with that, especially since it will “short-circuit” and skip the check for void 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 a comparison can be made on the left side, which is Int? due to an optional call chain.
Similarly, you can do:
if array?.isEmpty == false { print("There are objects") } else { print("No objects") }
Note: you must explicitly compare with false here for this to work.
If you want to do something when the array is nil or empty, you have at least 7 options:
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 accurately reflects how you described it in English: "I want to do something special only when it's empty or zero." I would recommend you use this as it is easy to understand. There is nothing wrong with that, especially since it will “short-circuit” and skip the check for void if the variable is nil .