Khanh Nguyen Khanh Nguyen - 5 days ago 6
Swift Question

Does Swift support reflection?

Does Swift supports reflection, e.g. is the something like

valueForKeyPath:
and
setValue:forKeyPath:
with Swift objects? Actually does it even have a dynamic type system, something like
obj.class
in Objective-C?

Answer

Looks like there's the start of some reflection support:

class Fruit {
    var name="Apple"
}

reflect(Fruit()).count         // 1
reflect(Fruit())[0].0          // "name"
reflect(Fruit())[0].1.summary  // "Apple"

From mchambers gist, here: https://gist.github.com/mchambers/fb9da554898dae3e54f2

Comments