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.

About Me

I teach Computer Science to college students, develop software, podcast, and write books about programming including the Classic Computer Science Problems series. I'm the publisher of the hyper local newsletter BTV Daily.

You can find me on Twitter and GitHub. Check out my podcasts Kopec Explains Software and Business Books & Co. You can subscribe to my very low volume newsletter to find out about my future book, media, or software projects.

Copyright

©2012-2023 David Kopec. As an Amazon Associate I earn from qualifying purchases.

Based on tdSimple originally by Lasantha Bandara and released under the CC By 3.0.