Apple took developers by surprise on Tuesday by announcing Swift, a new programming language for developing apps across Apple’s platforms.
Until now, most apps on Apple devices have been built using Objective-C, the language almost exclusively adopted by NeXT corporation years ago and subsequently by Apple.
Objective-C now seems to be on its way out, superseded by a language that looks to be "safe, modern and powerful", according to Apple’s engineering chief, Craig Federighi. But how powerful is this new language, and what difference will it make to the global community of developers building for Apple products.
What is Swift?
Swift is Apple’s new programming language, which has been in development for the past four years and which looks to replace Objective-C as the main language for app development on Apple’s platforms, OSX and iOS.
It’s a major departure from the syntax of Objective-C and takes a lot of cues from other languages, such as Haskell, C#, Ruby and Python, which Apple presumably hopes will make it appealing to bright young coders, keen on modern languages.
Although it’s a major departure, Apple have taken a lot of trouble to make the transition to Swift as painless as possible. It is fully binary compatible with existing Objective-C libraries and maintains a close relationship with the Cocoa frameworks.
That means that developers can introduce Swift into their apps at their own pace, by writing discrete modules that should seamlessly interoperate with their existing Objective-C code.
Importance for safety
Swift eliminates entire classes of unsafe code. Variables are always initialized before use, arrays and integers are checked for overflow, and memory is managed automatically. Syntax is tuned to make it easy to define your intent — for example, simple three-character keywords define a variable (var) or constant (let).
The safe patterns in Swift are tuned for the powerful Cocoa and Cocoa Touch APIs. Understanding and properly handling cases where objects are nil is fundamental to the frameworks, and Swift code makes this extremely easy. Adding a single character can replace what used to be an entire line of code in Objective-C. This all works together to make building iOS and Mac apps easier and safer than ever before.
What are the other improvements Compare to Objective-C?
Type Inference
In Swift there is no need to annotate variables with type information as the compiler can infer type based on the value a variable is being set to. Due to the dynamic nature of Objective-C, type is not truly known at compile time because methods may be added to existing classes, entirely new classes added or instance type changed all at runtime.
Type Safety
With Swift, the compiler can be more helpful in catching subtle type related bugs. As the compiler knows more about type in any method call, it can optimise certain call sites and jump directly to the implementation using C++ style vtable dispatch, rather than going through dynamic dispatch as in Objective-C. This enables smart optimisations that can make code run faster.
Control Flow
The humble switch statement has undergone a radical overhaul in Swift and can now match against ranges, list of elements, boolean expression, enums amongst others. It doesn’t fall through by default, and is further enhanced by Swift’s flexible pattern matching.
Optionals
An optional type is a type that might contain a value of a type. It allows you to more easily convert between types and avoid null checks. Optionals can be chained together to protect from exceptions when calling multiple methods or properties in a chain where one call might return “nil”.
Strings
Strings are now easier to deal with in Swift, with a cleaner syntax than Objective-C, eg: concatenate strings using “+=“.
Best to ios Games
As alluring as the 'write once, run anywhere' promise is, apps based on web technologies have historically performed less admirably than their native counterparts. These performance issues have made it impossible to use web technologies for some types of apps. Games, in particular, are notoriously picky about responsiveness and developers often need to squeeze as much performance out of the CPU/GPU as possible. It's likely no accident that Apple chose to showcase a game for their programming demo.
However, the overwhelming majority of developers aren't creating games. Furthermore, many developers aren't launching their apps with a high degree of certainty. In the beginning, the goal is adoption and learning. During this formative stage, developers aren't doing themselves any favors by committing to a platform-specific language and limiting their potential audience.
At the same time, HTML keeps getting better and better. In fact, during WWDC, Apple announced that iOS8 would extend the performance benefits of Safari to all Webkit based apps. This enhancement is going to improve all apps based on web technologies.
Swift is a great move for Apple and exciting news to developers who are willing to commit exclusively to iOS. But most mobile developers are better served by cross-platform technologies which enable them to engage with everyone; not just the Apple crowd.