Skip to content

Instantly share code, notes, and snippets.

@layoutSubviews
Last active January 2, 2020 05:39
Show Gist options
  • Save layoutSubviews/4f06aac1ba2bf26a1da9f020103afb55 to your computer and use it in GitHub Desktop.
Save layoutSubviews/4f06aac1ba2bf26a1da9f020103afb55 to your computer and use it in GitHub Desktop.
import Combine
func stringIfOver42(_ value: Int) -> String? {
value > 42 ? "\(value)" : nil
}
let voidSubject = PassthroughSubject<Void, Never>()
let intSubject = PassthroughSubject<Int, Never>()
let publisher = voidSubject
// Changing "String?" to "String" below fixes the bug.
.map { () -> AnyPublisher<String?, Never> in
intSubject
.compactMap { stringIfOver42($0) }
.eraseToAnyPublisher()
}
.switchToLatest()
let cancellable = publisher.sink { print($0) }
voidSubject.send(())
// This should not print anything, but does
intSubject.send(10)
// This should print "50"
intSubject.send(50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment