//doublslash

Technology Tear-down.

Embrace the Swift

Last week was Apple’s week as it unveiled series of surprises for users and developers alike, in its annual developer conference WWDC. While most of us had our “oh-aah” moments seeing the grand unveil of OS X Yosemite, the 10th update to the OS X, and iOS 8, a more or less an incremental update to iOS 7, if we ignore the number for a moment. Some of us saw the event as a platform to brag about how innovative Apple is (or was ?) and very next moment showing off features stolen from competitors. Though the biggest surprise that came with this year’s WWDC is that it finally became a Developer Conference in its true sense.


WWDC 2014 Banner (courtesy: Apple)

While OS X has not been a headache to its users at all (myself being a fan of), Apple did broke the great wall guarding around iOS that it have had for years and gave users plenty of reasons not to switch to a more open garden like Android. While, I would skip giving my thoughts on iOS 8 or Yosemite comparing it with Android or Windows to avoid any internet outrage. I was thrilled in particular by one thing that came up with a big surprise for developers, a new programming language called Swift.


Apple had the same native programming environment built on top of Objective C for more than 30 years, dating back to its origins in Stepstone in early 1980s. While not only the foundations of OS X and iOS; Cocoa and Cocoa Touch were written in Objective-C, developers had this language as an only option if they wanted to create a “true” app for any of the two platforms. But why a new programming language after 30 years? did Obj-C suck all of a sudden? or there are obvious advantages with Swift? let’s dig in!

Firstly, let’s have some idea about Obj-C. It not only has “C” in its name, but does share a lot with C language itself, more or less, Obj-C is a superset of C. What’s more, an Obj-C compiler would let you throw in C code without a hitch! But as far as similarity between the two goes, it is more of what’s inside rather than external appeal, since creators of Obj-C were inspired heavily from Smalltalk, a language created at Xerox (Apple has this unique history with Xerox on many aspects). So syntactically, Obj-C is not very identical with C, in fact, a seasoned C programmer would find alienated in an Obj-C environment.

Apart from just different syntax, Obj-C brings a whole new set of terminologies, which would otherwise sound out-of-context in other programming environments, an example; Interfaces in Obj-C are known as Protocols, Namespaces/Packages are known as Categories and many more. Unlike C, Obj-C is dynamically typed, a biggest difference. While Garbage Collector wasn’t available to the language until version 2.0, iOS implementation of Obj-C runtime never got a Garbage Collector (instead it has ARC - Automatic Reference Counting, which carries its own problems), a headache to lazy developers who come from more comfortable environments like .NET or Java where they don’t have to worry about objects’ after-life.


Now focussing on Swift, it is not a “fresh” language either, in fact, development of Swift reportedly started back in 2010. Surprisingly, Apple managed to keep this a secret for all these years (or that anyone really cared for anything except iPhone, duh!). So what makes Swift a breakthrough? it looks SO FAMILIAR. This is the place where Apple copied something for good. Looking at the syntax of Swift, and as I’d quote my own tweet “Apple’s new programming language, Swift, looks like some crazy nerd combined NodeJS, ECMAScript 6 and bit of Rust.”


Apple’s new programming language, Swift, looks like some crazy nerd combined NodeJS, ECMAScript 6 and bit of Rust.


Swift Logo (courtesy: Wikipedia)

Swift borrows lot of the syntax from, JavaScript and Rust (a language from Mozilla)! though it has embraced the syntax of a more modern implementation of JavaScript, which is ECMAScript 6, for function declarations, iterative structures like for loops, etc. Take a look at the sample program coded in swift.

	// methods and functions are declared with the "func" syntax
	// note the use of parameter naming, and the return type specified with ->
	func sayHello(personName: String) -> String {
	    let greeting = "Hello, " + personName + "!"
	    return greeting
	}
	 
	// prints "Hello, Jane!"
	println(sayHello("Jane"))

Apart from just syntactical familiarity, Swift has a huge performance boost over Obj-C, thanks to the underlying LLVM. Besides just performance, it doesn’t allow pointers, neither it supports ability to embed C code, which Obj-C did, while these limitations add up to the safety of your running code, they do impose limitations in certain scenarios. Also, Swift greatly simplifies string mutations, which used to involve a lot of work with Obj-C, by performing a simple string concatenation as you would do in any other language, for example; var str = “hello”; str += “world!”; Though one of the biggest feature that Swift carries, unlike its influencers, is ability to overload operators, and thus keeping its roots to Obj-C raw nature.

While Swift is interesting on its own, it carried bunch of goodies to the core SDK, SpriteKit and SceneKit in particular, that will enable indie developers to create casual games without a need to know any gaming engine, which was otherwise impossible.


Although Swift looks very exciting, it is still not fully finished, as simple as object scoping lacks in Swift, it still relies on good ol’ ARC for memory management. But what does it mean for Obj-C ? a death of a three decade old programming language ? Not at all. Swift is not here to replace Obj-C anytime soon, let’s not forget that there are over 9 million apps in AppStore written entirely in Obj-C, and hence nobody is rewriting all of that. But Swift will act as an alternative to Obj-C for developers who come from a more mainstream background. And hopefully in the years to come, Apple may break the shell and allow development in other platforms through Swift.

blog comments powered by Disqus