When it comes to developing software in the Apple Universe (OS X and iOS), Objective-C is now the “old and busted” while Swift is the “new hotness”.
There are already dozens of resources on the Web for learning how to create “Hello World” in Swift, declare a variable, write a function, and so on. Apple has made available three wonderful documents that cover the essentials:
- The Swift Programming Language
- Swift Standard Reference Library
- Using Swift with Cocoa and Objective-C
There is also no shortage of people trying to distill the hotness into a Twitter-like 140 characters or less.
- Industrial-quality systems programming language that is as expressive and enjoyable as a scripting language.
- A modern, safe, fast, interactive, and interoperable language used to write code for Apple’s platforms.
- Objective-C without the C.
- Freedom from square brackets.
- The ability to combine two strings by using the + operator (I always felt bad explaining the absence of that one to new Objective-C programmers).
Personally, I’m a long-time Objective-C programmer who still actually enjoys using the language. Of course, I also very much enjoy writing C++ so maybe I need some professional help.
Anyway, my goal with this series of articles is not to give you a basic tutorial on how to write Swift. As I said, there are more than enough places to go for that. What I will do is give you some insight into what Chris Lattner and his compatriots at Apple were thinking when they came up with this little gem. I will focus less on the “how” and shine a brighter light on some of the “why”.
- Why should I use a constant instead of a variable whenever possible?
- Why are there implicitly unwrapped optionals in Swift and why are they so darn confusing?
- Why should I use Swift if I can still use Objective-C and love it so?
I will also occasionally take you behind the curtain and explore some of the things that make Swift tick.
- What is CIL, LLVM IR, and why should I care?
- Is Swift really faster then Objective-C and how did they do that?
Most importantly, I want to help you to learn to “think in Swift”. No one likes to be the foreigner in a strange land speaking the language in that “not quite right” sort of way. The Python community uses the term “pythonic” to refer to code written the “right way” and not in a way that makes them say, “Look, how cute, another C programmer trying to write Python code”. Looking like a Swift native speaker will require you to think different at times (wouldn’t want it any other way) but it will hopefully be a rewarding and enjoyable experience. Swiftic? Swiftonic? Swifty? I think I’ll stick with “thinking in Swift”.
If you’d like to dive right in with a discussion about the different ways to write Swift and the Swift compiler itself, feel free to move on to Part II. If not, stick around and take a brief journey with me through some of the relevant history leading up to the announcement of Swift.
In 1996, Apple acquired NeXT Computer, Inc. As a result of that deal, Apple ended up with new leadership (Steve Jobs – again), a new operating system (OS X), and a programming language (Objective-C).
Objective-C was actually developed in the early 1980s and selected as the main language for the NeXT OS in 1988. The Foundation and AppKitsoftware development frameworks were also initially developed at NeXT and this is why many class names begin with the letters “NS” for NeXTstep.
While some developers really do like Objective-C (including yours truly), others have called it everything from an outdated language to an abomination. In truth, the developers at Apple were playing the hand they were dealt and they made the best of it by adding new modern language features on a regular basis:
- Dot syntax
- Automatic Reference Counting (ARC)
- Collection literals
A significant step in the evolution of Objective-C at Apple was the move from the GCC compiler to the LLVM compiler infrastructure and the Clang compiler front end. The initial design and implementation of LLVM was the work of Chris Lattner while a research assistant at the University of Illinois at Urbana-Champaign (early 2000).
As an interesting aside, Chris worked as a research intern for Microsoft Research during the summer of 2004 where he built an experimental bridge between the Microsoft compiler and the LLVM compiler allowing LLVM to compile and run .NET code.
In 2005, Chris was hired by Apple and began the process of preparing LLVM for use in Apple products. Eventually, enough of the pieces were in place for a more significant change.
In July of 2010, Chris began work on a new programming language (Swift) that could take maximum advantage of the LLVM infrastructure. Swift became a major focus of the Apple Developer Tools group in July 2013 and was officially unveiled to the public in June 2014.
Author: Jason Bell, Accelebrate’s Director of Technology and senior instructor.