Archive for August 2015

Why I Write in Pure Swift

I have had the pleasure of working on several Swift data structure and algorithm libraries over the past year, including SwiftGraph, SwiftPriorityQueue, and SwiftCSP. I wrote all of them in what I call "Pure Swift" (and maybe others are calling it that too). By Pure Swift, I mean Swift code that utilizes only the Swift standard library with no external dependencies and no use of Cocoa or Cocoa Touch (including Foundation).

Why write in Pure Swift? Swift has a small standard library, so sometimes being Pure Swift can seem somewhat limiting. Apple has already told us that they will be open sourcing Swift and releasing a version for Linux. It's a near certainty that the release will not include a version of Cocoa for Linux (although there is GNUStep and they say they will have bindings). So, if you're working on a project in Swift that doesn't need to make use of Cocoa and you want it to be portable to future incarnations of Swift (including those on other platforms), then you should write it in Pure Swift.

There's also the possibility that the future will bring a new UI framework from Apple that is written in Swift to begin with to supersede Cocoa, but I wouldn't hold my breath after its near thirty year history. But, if that remote possibility is to come true, then once again you will want your existing projects as much as possible to be in Pure Swift. For the present, by writing in Pure Swift you get automatic cross-platform compatibility between OS X and iOS.

Finally, there's something that just seems hobbled when you rely on too many Objective-C classes in your Swift code. It's hard to make value types work well with them, and there are several other incompatibilities like tuples.  I love Objective-C, but I want to keep my non-UI Swift code pure.

Posted in , , , , , , , , | Leave a comment
Copyright 2012-2016 David Kopec. Powered by Blogger.


Swedish Greys - a WordPress theme from Nordic Themepark. Converted by