Transforming a function's return type from AnyPushliser<ReturnObj, error> to ReturnObj in Swift

Viewed 80

I am new to Swift Combine and re-writing the code for my functionality in Swift Combine where the function is returning AnyPublisher<Person, Error>.

public func newPublisherFunction() -> AnyPublisher<Person, Error> {
   .....
}

However, the callers of my code are still on Obj-C (even Swift too but not using Combine) and the function which they are invoking, in my code presently, returns a direct object type Person as shown below

// this is the function being used by different callers
public func existingFunction() -> Person {
   ....
}

Is there a way possible to create a bridging function which can take value (of type Person) emitted by someFunction1() and just return that value.

Specifically asking, I want to implement something like below

public func newPublisherFunction() -> AnyPublisher<Person, Error> {
   .....
}

func bridgingFunction() -> Person {
   var person: Person

   // logic to consume value from newPublisherFunction()
   // and assign it to 'person'

   return person
}

public func existingFunction() -> Person {
   return self.bridgingFunction()
}
0 Answers
Related