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. あなたのコレクションが空かどうか確認する必要がある場合、isEmptyプロパティを使ってください、countプロパティがゼロと等しいのを調べるのではなく。RandomAccessCollectionに準拠しないコレクションに対して、countプロパティにアクセスすることはコレクションの要素を始めから終わりまで反復します。


let horseName = "Silver"
if horseName.isEmpty {
    print("My horse has no name.")
} else {
    print("Hi ho, \(horseName)!")
}
// Prints "Hi ho, Silver!")

Complexity: O(1) 計算量:O(1)