iOS Combine (SwiftUI)
[iOS - SwiftUI] Combine Mathematical 연산자 (count, max, tryMax, min, tryMin)
jake-kim
2022. 11. 10. 22:02
목차) Combine - 목차 링크
Math 연산자
- .count()
- 의미 그대로 배열의 갯수를 반환
- max(by:)
- by에 해당하는 조건을 갖는 최댓값 반환
[(1, 2), (1, 3), (1, 4)].publisher
.max(by: { $0.1 < $1.1 })
.sink { print($0) }
// (1, 4)
- tryMax()
- throw를 던질 수 있는 max 연산자
struct MinusError: Error {}
[1,2,-1,3,4].publisher
.tryMax { first, second in
// first, second = (1, 2), (2, -1), (-1,3), ...
guard first != -1, second != -1 else { throw MinusError.init() }
return first < second
}
.sink(
receiveCompletion: { print("receiveCompletion: \($0)")},
receiveValue: { print("recieveValue: \($0)") }
)
// receiveCompletion: failure(ExMath.MinusError())
- min(by:)
- max(by:)에서 by의 조건이 반대이므로 주의
[1,2,3,4].publisher
.min(by: { $0 < $1 }) // max(by: { $0 > $1 })와 동일
.sink { print($0) }
// 1
- tryMin()
- 예외를 던질 수 있는 min
struct MinusError: Error {}
[1,2,-1,3,4].publisher
.tryMin { first, second in
// first, second = (1, 2), (2, -1), (-1,3), ...
guard first != -1, second != -1 else { throw MinusError.init() }
return first < second
}
.sink(
receiveCompletion: { print("receiveCompletion: \($0)")},
receiveValue: { print("recieveValue: \($0)") }
)
// receiveCompletion: failure(ExMath.MinusError())