How to deal with the lack of reflection in Swift?

As an experienced Objective-C developer who is currently learning Swift, I really miss some of the reflected and dynamic functions of Objective-C.

For example, I wrote a JSON serializer that automatically matched keys and values ​​using the KVO and Objective-C introspection, and there are open-source libraries like Mantle that do this.

I can declare my object a subclass of NSObject and continue, but I feel this is not a quick way to do things.

Is there any other way to accomplish the same tasks avoiding patterns using what Swift offers?

+4
source share
1 answer

EDIT: (2016) this answer is automatic. Some of the recommendations may still be relevant, but now that Swift is open source, I would consider other possible answers.

There is no native reflection of KVO, like what you described is built into Swift. See: fooobar.com/questions/32820 / ...

And based on what we know about how the Swift compiler optimizes method execution at compile time (compared to a pure implementation of ObjC execution), it looks like it is unlikely to be added any time soon. See fooobar.com/questions/32815 / ... and http://blog.untitledkingdom.co.uk/obj-c-vs-swift/ for more information about this.

With all that said, here's a blog post about some of the KVO alternatives in Swift: http://blog.scottlogic.com/2015/02/11/swift-kvo-alternatives.html and another that describes some reflection options, which are in Swift: http://freecake.angelodipaolo.org/simple-reflection-in-swift/ .

+3
source

All Articles