jeudi 30 mars 2017

Swift equivalent of C#'s IsAssignableFrom

In C#, given two types, I can say something like

var stringType = typeof(string)
var intType = typeof(int)
var assignable = stringType.IsAssignableFrom(intType)

The value of assignable would be false since an int cannot be assigned to a string.

Is there any way to do this in Swift?

func isAssignable<T1, T2>(_ type1: T1.Type, from type2: T2.Type) -> Bool {
    // What's the implementation here?
}

let assignable = isAssignable(String.self, from: Int.self)

My suspicion is that this is not currently possible.





Aucun commentaire:

Enregistrer un commentaire