Does Swift support reflection?

Khanh Nguyen picture Khanh Nguyen · Jun 5, 2014 · Viewed 34.3k times · Source

Does Swift support reflection? e.g. is there something like valueForKeyPath: and setValue:forKeyPath: for Swift objects?

Actually does it even have a dynamic type system, something like obj.class in Objective-C?

Answer

stevex picture stevex · Jun 5, 2014

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