Instance Property

isEmpty

A Boolean value indicating whether the collection is empty.

Declaration

var isEmpty: Bool { get }

Discussion

When you need to check whether your collection is empty, use the isEmpty property instead of checking that the count property is equal to zero. For collections that don’t conform to RandomAccessCollection, accessing the count property iterates through the elements of the collection.

let horseName = "Silver"
if horseName.isEmpty {
    print("I've been through the desert on a horse with no name.")
} else {
    print("Hi ho, \(horseName)!")
}
// Prints "Hi ho, Silver!")

Complexity: O(1)

See Also

Inspecting a String

var isEmpty: Bool

A Boolean value indicating whether a string has no characters.

var count: Int

The number of elements in the collection.

var count: Int

The number of characters in a string.