Given an existing CurrentValueSubject
instance, the goal is to create a new Publisher
that will take the subject's stream of Strings, and in turn will output Ints.
My approach is to map the subject to AnyPublisher
:
let subject: CurrentValueSubject<String, Never> = ...
func intPublisher() -> AnyPublisher<Int, Never>
{
return subject.map { string in
let value = Int(string) ?? 0
return AnyPublisher<Int, Never>(value) // Error: Generic parameter P could not be inferred.
}
}
However, I cannot create a new AnyPublisher
this way. Clearly, my understanding of Publishers is flawed. Could someone please enlighten me?