mardi 10 juillet 2018

Reflection on AnyObject in Swift

protocol Protocol {}

struct Bar : Protocol {
    let bar: String
}

let bar = Bar(bar: "bar") as Protocol

for (label, value) in Mirror(reflecting: bar).children {
    print("property: \(label!), value: \(value)")
}

prints

property: bar, value: bar

whereas

struct Baz {
    let baz: String
}

let baz = Baz(baz: "baz") as AnyObject

for (label, value) in Mirror(reflecting: baz).children {
    print("property: \(label!), value: \(value)")
}

prints

 

(well, nothing.)

I can't see how an empty protocol changes the ability to reflect on an object. Is reflection on AnyObject generally not possible? If so, why is that?





Aucun commentaire:

Enregistrer un commentaire