The Swift Programming Language

The Swift Programming Language

Posted on 10 June 2014 | Reading time: 4 minutes

This year’s WWDC Keynote, the main event at Apple’s yearly developer conference, has been mind-blowing. The company has released an impressive amount of tools and APIs for iOS 8 and OS X 10.10 Yosemite. They say it’s the biggest release since the iPhone SDK was originally introduced… and I believe that!

I’ve been following the WWDC for a few years now, and every time Apple is able to surprise me. Most of the Keynote has been quite standard: updates about how the company is doing, new operating system for the Mac with fancy new features and UI, new operating system for mobile devices (iOS 8), etc. But then something happened: the last 20 minutes or so have seen Craig Federighi (Senior VP of Software Engineering) announce a bunch of new frameworks and tools for developers. So what? - you would say. Well, what has really been a surprise to all (you can tell by the loud ‘wow’ from the on-site audience at the Moscone Center, where the conference was held), has been the introduction of a new programming language for iOS and OS X development, Swift.

##A modern programming language

I don’t know about you, but while I was watching the Keynote that announcement literally kept me with my mouth open for a good five minutes! A new programming language?? When you’ve got Objective-C?? What do you need that for?!?! :anguished:

Swift is an innovative new programming language for Cocoa and Cocoa Touch. Writing code is interactive and fun, the syntax is concise yet expressive, and apps run lightning-fast.

I’ve been in love with Objective-C from the moment I first started developing for iOS and OS X: the syntax is clear, the inline parameters make it much more readable than any other language out there and the naming conventions used throughout the frameworks are fantastic. Thus, when Swift was announced I didn’t really know what to think!

I need to admit that I was a bit shocked and sceptical at first, but now I’ve got to say I’m impressed by Swift. It resolves many problems that Objective-C inherited from C and makes sure, by design, that programmers avoid some common mistakes. Moreover, although Swift’s syntax is very different and more compact, it keeps some of the best qualities of Objective-C and you can use all of Cocoa and Cocoa Touch frameworks with it. My favourite feature is named parameters:

func join(string s1: String, toString s2: String, withJoiner joiner: String) -> String {
	return s1 + joiner + s2
}

join(string: "hello", toString: "world", withJoiner: ", ")
// returns "hello, world"

Without this feature I would have hated writing code, honestly! It’s my biggest complain against all other languages out there: the ability to clearly specify what each parameter of a function is for. That’s invaluable.

Anyway, Apple has released an amazing 600-pages-long book about The Swift Programming Language on iBooks. Its entirety has been pretty much summarised with the WWDC sessions for developers - which I recommend you go and watch if you’re a developer enrolled in one of the Apple Developer Programs - that are now available on the developers portal.

I watched the videos and I’ve been reading the book for the last few days. I’ve not yet finished it and there are still a few areas that I need to clarify for myself (e.g. I still have to understand how - if at all - can we declare private properties and methods). One thing that really surprised me, though, is how similar structs and classes have become. In fact, in Swift structs can have stored values and can have methods and initialisers associated with them. Also enums can have methods and initialisers, coming closer to structs and classes in a certain way. Enums, though, are still very different compared to classes and structs. However, I’ve got the impression that the latter two will give an hard time to unexperienced developers: with such similar features I bet many will have troubles deciding whether to implement something using a struct or a class. In fact, I’m a bit puzzled myself about what the right direction should be. But let’s leave this topic for another time.

##Swift and Objective-C

So, Swift is all good and nice and people are already building Flappy Bird clones with it, but what about Objective-C? All Apple’s frameworks, all apps and third-party libraries are written in Objective-C. Shall we throw everything away? No, in fact:

Swift is ready for your next iOS and OS X project — or for addition into your current app — because Swift code works side-by-side with Objective-C.

But how does that work? Apple has released another document about it: Using Swift with Cocoa and Objective-C. In short, Objective-C headers are automatically translated to Swift and vice-versa when you add your first Swift file to an Objective-C project. You only need to import your Objective-C module or the generated Swift header into the appropriate files and that’s it. You write code as usual.

However, although far away then being deprecated, it looks like Objective-C is going to be abandoned in favour of this younger, modern language. I’m sure Apple is going to push switching to Swift only apps in the upcoming future and therefore it would be wise to start using this new language as soon as possible. A good comparison of what is likely to happen to Objective-C is one I’ve heard somewhere, probably in a podcast, right after the WWDC: it’s likely that Objective-C will undergo the same process of .xib files for Interface Builder that, although available, are de facto deprecated in favour of Storyboards.

What do you think about it? Tell me in the comments!