Dynamic Casting in Swift

We’ve been spoiled by Objective-C’s runtime flexibility. Its tendency for dynamic dispatch, as well as a very powerful runtime (that’s exposed via both Objective-C and C APIs) has meant that dealing with arbitrary data was an easy task. You just check [object isKindOfClass:klass] and accept id parameters and everything works as expected.

In Swift, however, we have a statically-typed language where the compiler really needs us to know types at compile-time, rather than runtime. In general, this helps us write safer code than was possible in Objective-C, but makes dealing with data this is fundamentally untyped a real challenge. Swift has two operators that help to bridge the gap between static safety and the power of dynamicism: as and as?.

Read More


I am so incredibly happy to announce today that I will be joining the fine folks at Realm starting on Monday. I’ll be working on building cool things there—CocoaPods, jazzy, and RestKit, along with working on Realm, the database, itself.

Read More

On Class Methods

Class methods, aka those declared with a + in Objective-C, are horribly misused. In most languages I deal with, there is a difference between methods and functions–functions are used either as a map in the mathematical sense, or to imperatively perform an action, while methods are bound to a given ‘receiver’, and thus the actions they expose are something the object they are invoked on performs, and any map they represent is a transformation of that object into one of its properties.

Read More