mercredi 13 juillet 2016

Mapping in Swift Between Protocol Conforming Types

I want to map between any two object which conform to the same protocol. It would be convenient to do so via a function with the signature:

func mapFrom<T>(objectA: T, to inout objectB: T)

Even better though (for immutable types would be to have it like):

func map<T, U: T>(from source: T) -> U

where somehow it could initialize a U object from the values in T.

I would like to do this via Swift Reflection rather than using the Objective-C run-time, but I would settle for that if it was the only way. If somehow it could be done without reflection that would be amazing, but I don't see how.

The reason I want to do this is because I have a mutable Realm classed which conform to their respective protocol, and I want to map them to the immutable struct types.

How can this be done, if it can be done at all?





Aucun commentaire:

Enregistrer un commentaire