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

Getting Information About the Buffer

var first: UInt8?

The first element of the collection.

var last: UInt8?

The last element of the collection.

var underestimatedCount: Int

A value less than or equal to the number of elements in the collection.

func max() -> UInt8?

Returns the maximum element in the sequence.

func max(by: (UInt8, UInt8) -> Bool) -> UInt8?

Returns the maximum element in the sequence, using the given predicate as the comparison between elements.

func min() -> UInt8?

Returns the minimum element in the sequence.

func min(by: (UInt8, UInt8) -> Bool) -> UInt8?

Returns the minimum element in the sequence, using the given predicate as the comparison between elements.

func distance(from: Int, to: Int) -> Int

Returns the distance between two indexes.

func allSatisfy((UInt8) -> Bool) -> Bool

Returns a Boolean value indicating whether every element of a sequence satisfies a given predicate.