Categories
Featured iPhone Development Resources iOS Development Tutorials Swift

Tutorial: Using Generics To Implement KVO In Swift

One of the issues you’ll notice when using Swift is that native key-value-observing is not available and only works with NSObject subclasses.

Swift is changing rapidly and a solution does appear to be on the roadmap for this issue. Until the solution arrives here’s a tutorial from Krzysztof Zablocki illustrating a nice technique for implementing observable properties completely using Swift. This technique uses generics and what’s really nice is that you can create the observable properties without affecting the rest of your code.

You can find the tutorial on the Merowing blog.

A playground demonstrating the technique can be found on Github here.

A nice simple technique for implementing KVO in Swift.