first는 이름그대로 첫 번째 인자의 값만 발행하고 나머지는 무시하는 Operator에요.
[1, nil, 2, nil, 3, nil, 4, nil, 5].publisher
.first()
.sink { completion in
print(completion)
} receiveValue: { value in
print(value)
}
.store(in: &cancellables)
// 결과
Optional(1)
finished
이때 주의해야 하는 점은 first() 는 Optional Publisher를 리턴하기 때문에 Optional 처리가 따로 필요할 수 있어요
public func first() -> Optional<Publishers.Sequence<Elements, Failure>.Output>.Publisher