iOS Combine (SwiftUI)
[iOS - SwiftUI] Combine Matching 연산자 (contains, tryContains, allSatisfy, tryAllSatisfy)
jake-kim
2022. 11. 12. 23:46
목차) Combine - 목차 링크
Matchging 연산자
* Combine에서 제공하는 Matching 연산자는 기존 Swift에서 제공하는 연산자의 인터페이스와 동일하게 설계되어 사용하기 쉽도록 구현
- contains(where:)
- where 클로저 조건에 따라 Bool 반환
[1,2,3,4,5].publisher
.contains(where: { $0 == 3 })
.sink { print($0) }
// true
- tryContains(where:)
- contains는 true를 반환하면 이후 값에 관해서 체크하지 않음을 주의 (아래에서 true를 먼저 반환하므로 throw 발생 x)
[1,2,3,4,5].publisher
.tryContains(where: {
guard $0 != 5 else { throw SomeError() }
return $0 == 3
})
.sink(
receiveCompletion: { print("completion: \($0)") },
receiveValue: { print("value: \($0)") }
)
// value: true
// completion: finished
- allSatisfy()
- 클로저안의 모든 조건이 true이면 true반환
[1,2,3,4,5].publisher
.allSatisfy { $0 < 6 }
.sink { print($0) }
// true
- tryAllSatisfy()
- tryContains(where:)와는 다르게 모든 값들을 비교해야하므로, throw 발생
[1,2,3,4,5].publisher
.tryAllSatisfy {
guard $0 != 5 else { throw SomeError() }
return $0 < 6
}
.sink(
receiveCompletion: { print("completion: \($0)") },
receiveValue: { print("value: \($0)") }
)
// completion: failure(ExMatching.SomeError())