Earlier this week, Apple announced some big news: There’s a new programming language named Swift, and it seems to be a huge leap forward for iOS development. Swift brings together many safety and convenience concepts and represents Apple’s first new language since the never-finished Dylan project of the mid-1990s.
First, a little history: Objective-C has been the preferred language for iOS and Mac development for about the last 14 years, ever since Mac OS X was released. Over those years, Objective-C has had some significant updates, most notably the addition of Blocks in 2010 and then Automatic Reference Counting in 2011. Objective-C’s balance of simplicity and power has served the developer community extremely well, but with Swift, I feel like I’ve seen the future of iOS and Mac development. (I worked at Apple as a senior iOS developer from 2006 to 2010.)
When writing code in Swift, it’s much more difficult for a developer to make common errors. The Swift compiler prevents entire classes of bugs which were previously very easy to introduce in C or Objective-C. For example, you can no longer forget to initialize a variable, thereby leading to nasty bugs caused by junk data. All data mutability is explicit, which means the programmer must go out of her way to mark data as being changeable. By reducing the amount of mutable state, another entire class of software bugs is mitigated. Recall Apple’s “goto fail” SSL bug from February 2014. It’s worth nothing that this bug wouldn’t have been possible if the code had been written in Swift, as it requires braces within a switch statement. And the compiler forces the programmer to explicitly handle all possible values, and without any implicit fall-through. I’ve touched on just a couple of Swift’s myriad safety features, and it is clear that Swift will produce safer code than is typical of C and Objective-C.
Swift also allows developers to be more productive by eliminating some of the more cumbersome programming tasks and also by adding lots of convenient features. The compiler’s type inference frees programmers from having to re-specify data types throughout their code. Header files are no longer needed. Tuples allow for multiple, named returned values. Structs and enums can be extended. Swift also brings optionals, generics, pattern matching, shorthand syntax, and many other niceties too numerous to list here.
To be clear, none of Swift’s concepts are new; all have been borrowed from other existing languages like Rust, Objective-C, Ruby, and Python to name but a few. But what differentiates Swift from existing languages is that it is built atop Apple’s Objective-C runtime, which means Swift is already compatible with all of the existing Mac and iOS frameworks. Developers can mix and match Swift and Objective-C, so we can get our feet wet without having to dive in.
For example, our Swift code can present UI using UIViewController and run methods on a background thread using Grand Central Dispatch, just as we did before. Since the framework APIs haven’t changed, existing iOS developers need only learn some new, mostly straightforward syntax in order to get started with Swift.
Finally, Swift also lowers the barrier to entry for aspiring developers to get started writing code for Apple’s platforms. With its playgrounds for language exploration and its REPL (Read-Evaluate-Print Loop), beginners can experiment quickly and repeatedly. And like many modern languages, beginners won’t initially have to worry about memory management.
After spending just a tiny amount of time reading about Swift and attending some WWDC sessions, I’m incredibly optimistic about how Swift can improve the experience of writing software for iOS and the Mac. Of course, it’s very early to be so excited. However, Swift brings so many clear advantages that I’m wholeheartedly embracing this brave new world. I can’t wait to get started with Swift.
You can find Mallory Paine on Twitter here.
[Image: Flickr user Keng Susumpow]