Apple first promoted Swift by asking the following question: ‘What would Objective-C be like without the baggage of C?’. The Swift programming language is used to build iOS apps, rich windowed Mac OS and console applications, and has apparently been under development for 3 years.
What’s happened is that Apple engineers have discarded the old, blocking elements, resulting in a brand new, fast and fairly simple product that makes a developer’s life easier. It’s taken the best of other popular languages, yet has undoubtedly taken a new direction. So, really, it’s Objective-C, without the baggage of C, with additional pro-features.
Swift is Apple’s flagship project. Clearly it aims to bring more developers into their platforms and to enable them to produce faster, more stable and reliable programs, with increased productivity in a fast and modern environment. It delivers a fair solution to developers who would like to avoid polluting the source code with bloated, secondary operations which hides the true meaning of the program flow.
Why is it so powerful? Well, Apple have taken their existing Objective-C runtime and built a language which has the ability to use the same underlying foundations, but in an optimized way. The language provides a number of new features which all help the compiler and the runtime to be faster and safer than before. Among others, strongly-typed collections, generic data structures and operations, compile-time null-checks with optional data types and much more clever closure statements are all big pluses. There are also native types and collections, concise syntax to deal with mutability all across the language, multiple return types, tuples, functional programming aspects, pattern matching, dedicated playgrounds and Read-Eval-Print-Loop environment to play with, and so much more.
So how fast is Swift? According to Apple, if Python is the reference point, Objective-C performs an average of ~3x that speed in a complex object sorting operation, while Swift does the same 4x faster. Taking the example of a computation heavy RC4 encryption mechanism, Objective-C handles it over 100 times faster than the Python, but Swift remains the clear winner: it handles it a mind-blowing 220 times faster. Perhaps you’re thinking, well, that doesn’t mean that much; any native language (which compiles to native code) would beat that Python score. Swift has been optimized to handle things faster in a number of ways compared to Objective-C, using runtime optimizations and gaining advantage of the new language-features.
The biggest challenge Apple have solved is to make Swift exchangeable with existing Objective-C code. Any existing Objective-C project can be extendable with new Swift files and classes. Furthermore any existing iOS or OS X framework handles both languages as they were made for them (because they were). Apple solved this by creating a bridge between old data structures and their new Swift equivalent, which enables the developer to use the best technology in any of the two languages. So the real advancement is that you are still able to use the well-known Apple frameworks, or every existing old code, while enjoying the new stuff as well.
Swift was designed to be strict but versatile, safe but powerful. With new syntactic and semantic elements it catches more errors that previously resulted in a crash at runtime, now unable to commit them. They are now revealed at compile-time. Swift took a step further and enabled developers to bring in new functional and reactive programming elements into the pragmatic environment, alloying their benefits in one single program.
Swift is still in its infancy, its adoption rate is relatively low, but it’s fair to say that more and more projects written in Swift are born daily. Some existing projects were already transformed and use the new language to gain more power and clarity, while some were started from scratch in Swift and became more and more popular. As an example, GitHub hosts more than 10k projects using the new language and 20 of them have already reached over thousands of stars. Swift’s development is fluent and fast, as the language has already hit the 1.2 version – albeit this was mostly about fixing bugs, though nonetheless bringing in more freedom and comfort to developers.
For those who are curious to learn more on Swift, I strongly suggest watching any of the introduction videos and tech talks around the internet –start with the one held at WWDC on the day of announcement, found here.
Author: Laszlo Teveli, Skyscanner Software Engineer