Apple: We'll tailor Swift to be a fast new programming language
Kiss goodbye to five years of Objective-C
WWDC Apple stunned the audience at its Worldwide Developers Conference in San Francisco on Monday with a tool few expected: a new programming language for iOS and OS X software called Swift.
There already is a programming language called Swift that was developed by the University of Chicago and Argonne National Laboratory for use in parallel computing applications. This isn't that.
What it is, is an entirely new syntax that – in the words of Apple senior VP Craig Federighi, who unveiled it during the Monday morning WWDC keynote – aims to be "Objective-C without the baggage of C."
Some of that "baggage" will already be familiar to developers who cut their teeth on C but later moved on to scripting languages such as Python (and Federighi compared Swift to Python several times during his presentation).
Like scripting languages but unlike C, Swift lets you get straight to the point. The single line
println("Hello, world") is a complete program in Swift. Note, also, that you don't even have to end the statement with a semicolon, as you do in C. Those are optional, unless you're combining multiple statements on a single line.
Swift also aims for a level of scripting language–like interactivity. At WWDC, Apple developer-tools chief Chris Lattner demoed coding using a feature called "playgrounds," which allow a developer to modify portions of a program in Xcode and see the results in an output window in real time.
Swift code outperforms Python by a wide margin, Federighi said, and even beats Objective-C
Unlike scripting languages, however, Swift is compiled, using a compiler based on the LLVM project (which Lattner created before joining Apple in 2005). That means programs written in it are fast. Compared to Python, Federighi said, a complex object sort written in Swift ran 3.9 times faster, and RC4 encryption ran 220 times faster. In fact, he said, the Swift code even ran faster than the same algorithms implemented in Objective-C.
Familiar, yet new
Swift is object-oriented, but it also supports some functional programming styles. Functions can be passed around as data, for example, and the language also supports closures.
It also adopts a number of modern language features, such as optional typing, generics, type inference, namespaces, and tuples as a data type. Identifiers can be composed of any Unicode characters – so you can name your variables in Chinese, for example, or even use symbols.
Swift has also been designed to make it easier for developers to write secure code. Federighi said the language's syntax "defines away large classes of common programming errors; they're just not possible."
According to Apple's developer documentation, Cupertino spent years developing Swift as a 21st-century language that's suitable for everything from short programs to entire operating systems.
"Swift combines the best in modern language thinking with wisdom from the wider Apple engineering culture," the manual not-so-humbly states. "The compiler is optimized for performance, and the language is optimized for development, without compromising on either."
At the same time, Swift is designed to be fully interoperable with Objective-C. Objective-C APIs can be called from within Swift code and vice versa. Code written in both languages can be mixed and matched within the same program, and migrating code from one language to the other should be easy.
That in turn means Swift already supports Apple's key development frameworks, including Cocoa and Cocoa Touch.
What you shouldn't expect from Swift, however, is a general-purpose programming language for use outside Apple's ecosystem. The fruity firm made no mention on Monday of how it plans to license Swift, whether it will release the source code to its compiler, or whether it will be possible to write code in Swift for platforms other than iOS and OS X – but we think maybe you shouldn't hold your breath for any of that.
In addition to its online documentation, Apple has released an e-book, The Swift Programming Language, that's a free download from the iBooks Store.
To start working with the language itself, you'll need to download the beta release of XCode 6, which includes tutorials to get you going. ®