본문 바로가기
Swift/일문일답

isEmpty 속성

by UDDT 2025. 2. 21.

(원문을 보고 정리한 내용으로 오역이 있을 수 있습니다.)

 isEmpty

  종류 : 인스턴스 속성(Instance Property)

  설명 : 컬렉션이 비었는지 아닌지에 대해 불린 값으로 나타내는 것

var isEmpty: Bool { get }

 

  count 속성을 사용해서 컬렉션 0과 같은지(비었는지)를 확인하는 대신, isEmpty 속성을 사용하는 것이 좋음.

  (RandomAccessCollection에 준수하지 않는 컬렉션의 경우(Set이나 Dictionary)는 count 속성을 사용하면

    순차적(혹은 순서가 없기 때문에 계속 반복하여 확인)으로 다 확인해야 하므로, 시간이 오래 걸림)

 

  - isEmpty 사용 예 : 

let horseName = "Silver"
if horseName.isEmpty {
    print("Please, Give the horse a name ")
} else {
    print("Hi ho, \(horseName)!")
}
// Prints "Hi ho, Silver!")

 

 - 시간 복잡도

     O(1)

 

⎮ 레퍼런스 보기

    https://developer.apple.com/documentation/swift/sequence/map(_:)#parametershttps://developer.apple.com/documentation/swift/array/isempty#discussion

 

isEmpty | Apple Developer Documentation

A Boolean value indicating whether the collection is empty.

developer.apple.com

 

 + 레퍼런스로 영어 공부하기

iterate 반복하다

 

'Swift > 일문일답' 카테고리의 다른 글

lowercased(), uppercased() 메서드  (0) 2025.02.24
randomElement() 메서드  (0) 2025.02.22
map(_:) 메서드  (0) 2025.02.20
append(_:) 메서드  (0) 2025.02.19
contains(_:) 메서드  (1) 2025.02.18

최근댓글

최근글

skin by © 2024 ttuttak