A Blog

Porting DB5 to Swift

August 12, 2014

I’m a big fan of using the DB5 library for customizing an iOS app via a plist configuration file, and I’ve used it in several projects so far. I’ve also been digging more into Swift and iOS 8 and thought it would be a good exercise to familiarize myself more with Swift to port DB5 from its original Objective C codebase to Swift.

The result is over on Github: DB5-Swift.

For grins, I wanted to go with a purely Swift approach, no NSWhatever classes unless there was no alternative. This meant using Dictionary instead of NSDictionary. Which turned into a bit of a headache. The biggest issue I ran into was that Swift’s Dictionary doesn’t seem to have an equivalent of NSDictionary’s initWithContentsOfFile:1. Rather than try to parse the plist by hand, I just went back with good ole NSDictionary.

Two of Swift’s headlining features are type inference and optional values. I’ve used both of these features before in C# for several years and have appreciated what they bring to the table. Swift is a little bit different though because it comes with the history of Objective C and Cocoa. NSDictionary was built to take advantage of Objective C’s type system, and using it with Swift involved more casting and ? and ! operators than I expected. It didn’t help that Xcode would often be wrong in its helper tooltips, suggesting addtional !‘s which wouldn’t fix the problem.

I’m not entirely sure if DB5-Swift is 100% up to snuff right now. I think some of the methods should have better handling for nil values. I still have plenty of learning left before I’d consider myself adept at Swift. Overall, it was a good exercise, and I will use it in future apps.

1. Not necessarily true according to @_lanceparker on Twitter.


Scott Williams

Written by Scott Williams who lives and works in sunny Phoenix, AZ. Twitter is also a place.