So among the announcements of this year’s Apple Developer Conference was the introduction of a new programming language called “Swift”. It must be said that this took most of the developer community by surprise.
Since the introduction of OS X and iOS, Objective-C/C has been the default programming language used to build apps on these platforms. This language has and still does serve us very well. But they come with a lot of baggage. Objective-C is over 20 years old and C over 30. It must also be said that the Objective-C syntax can be slightly confusing for new developers or developers who are cross training.
So for the past few years Apple has been developing a new, modern programming language, which Swift is the result of. It builds on the best bits of Objective-C/C without the constraints of C compatibility.
What Apple has tried to do, and has achieved, is to make the language simpler and shorter. The less you have to type the better. The code you write is also a lot safer, less prone to errors.
So here are a few hi-lights of this new language
You no longer need to terminate each line with a semi colon. Thats 10s of thousand of keystrokes saved.
With Objective-C/C, when you create a variable you always have to specify the type of value it stored, whether this was an integer, string or boolean. With Swift you no longer have to do this. Rather than the developer specify the type of data, the compiler infers this based on the data you pass to the variable.
Objective-C is very much a run-time language which means the type of a variable is not really known until the app is running. Swift on the other hand infers type during compilation. The upshot is the compiler can optimise the code much more efficiently, resulting in faster code execution. A win for everyone.
No More Header Files
With Swift, header files are no longer required. Your class interface and implementation are held within one file. These files have a .swift extension.
Functions and methods in Objective-C can only return one value. A tuple allows a Swift function to return multiple results.
Mix and Match
You can mix Objective-C/C code with Swift. So if you have an existing app, you don’t need to re-write it from scratch if you want to investigate Swift.
Enumerating through a collection, such as an array or dictionary, is now faster and more flexible
This is a new feature of the forth coming Xcode 6. This is how Apple describes Playgrounds.
Playgrounds make writing Swift code incredibly simple and fun. Type a line of code and the result appears immediately. If your code runs over time, for instance through a loop, you can watch its progress in the timeline assistant. The timeline displays variables in a graph, draws each step when composing a view, and can play an animated SpriteKit scene. When you’ve perfected your code in the playground, simply move that code into your project. With playgrounds, you can:
- Design a new algorithm, watching its results every step of the way
- Create new tests, verifying they work before promoting into your test suite
- Experiment with new APIs to hone your Swift coding skills
If you are an Objective-C developer there is no need to panic. Objective-C is still king, however Swift is clearly the future. For the foreseeable future, Apple will maintain parity between these two languages. One warning though. Apple did make it clear that Swift is brand new and that over time there may be some changes to the language that will require your source code to be modified.
For myself I will still be using Objective-C for all our existing projects and well as all our major projects but as soon as a small project hits my desk, I will be checking out Swift.
Swift will be available when OS X Yosemite and iOS 8 ships in the fall. At that point we will be introducing a number of new training courses based around Swift and iOS 8.
Interested in finding out more about Swift? Then take our “Beginning Swift Development” course. The first course of its kind in the UK!