First look: Apple's Swift is simple, at first

Swift's clean and modern syntax makes it quick and easy to get started, but mastering this new language will take a while

What is it about being a rich corporation? After the private jets and the gleaming headquarters comes the new programming language. Now Apple has followed in the path of Microsoft (C#), Sun (Java), and Google (Dart, Go) to offer us Swift, a language with a C-like syntax and inferred typing designed to make it easier for the average coder to start building new software for the Mac and new apps for the iPhone.

While it's never possible to overestimate the amount of hubris at Apple, Swift is more than a vanity project to dictate how the world speaks a computer version of the King's English, if you will. Apple's software stack has been stuck with Objective-C since NeXT invaded the company, and many neophytes find it a pain to untangle the pointers, the files (both .m and .h), and the punctuation-rich syntax.

[ Also on InfoWorld: 10 features Apple "stole" for the Swift programming language. | See InfoWorld's "iOS 7 for developers" special report for the scoop on the bells and whistles in Apple's mobile OS -- and how you can harness them. | Keep up with key Apple technologies with the Technology: Apple newsletter. ]

Apple tried to update the language with a "modern syntax" in 1997, but the masses refused to budge from the so-called classic syntax. Other projects -- like adding Java bindings for the Cocoa libraries -- never really stuck. Today, learning Objective-C is one of the biggest hurdles to getting people to code for the Apple platforms.

The good news: Swift will be a great gift for anyone longing to avoid the hackerish brambles of Objective-C. Programmers who learned Java for AP Computer Science, JavaScript to build Web pages, and Ruby to build websites will find plenty that's familiar to them. Many of the rough spots such as the multiple files and inheritance are smoothed over, and the punctuation symbols are less dominant. There are also plenty of automated features, including inferred typing and automatic reference counting that help the compiler snag many of the programmer's potential bugs.

Why Swift?Some may wonder why Apple didn't embrace one of the many languages already on the market. JavaScript would be a good option as the Web eats the world, but it doesn't offer all of the low-level access to bytes that system programmers need to deliver the slick interfaces that define the Apple experience. In any case, JavaScript programmers are already using frameworks like PhoneGap/Cordova. Other languages like Python or Ruby are well integrated with the Unix back end, but they lack deep integration with the system software. The best answer may be that Apple has made a huge investment in the Cocoa layer, and Swift is the best way to preserve it while offering modern conveniences.

The basic structure for a Swift program looks much like Java. The code is broken into classes filled with fields and methods. The most jarring difference is that the methods start with a keyword (func) and the return value comes after the method name, not before it. The classes can be aligned in a hierarchy with plenty of inheritance for object-oriented code, and the properties can be read with a dot syntax.

That high-level road map and a few details about the syntax of declaring variables is all you need to get going. Within a few days of Apple's announcement, open source software written in Swift started appearing on GitHub. One of the most impressive, a clone of Flappy Birds by Nate Murray, required just two major Swift files and a bit of glue code. If you know your way around OS X or iOS programming, you can get moving very quickly. Murray says he spent only a day on his clone.

Swift fixes a few of the biggest complaints about Java syntax. You won't need to waste keystrokes spelling out the type of each variable; the compiler will infer it from initialization. You get all of the advantages of letting the compiler check the types without many of the keystrokes. If you want to glue together variables and strings, a simple templating hack uses a mere three keystrokes to insert a variable into a string. It's a masterstroke.

There are other neat additions. Xcode now offers a "playground" where you can type code and watch it execute as your fingers move. Sandboxes like these have made learning JavaScript and many JavaScript APIs much easier. Google, for instance, has lured many developers to its Maps API merely by offering a Web page with a playground in it. Xcode's playground is even better than most because it displays a running interpretation of the code in a gray section to the right, letting you see how the language operates. The only negative I'll register about the playground is that mine crashed repeatedly in ways that couldn't be reproduced. It's not like I tried to divide by zero. It bonked, and the next time it worked.

For all of the advances and improvements, I found the size of Swift to be a bit daunting. While it's easy to see how Nate Murray knocked off Flappy Birds in a day, many other details of the language can't be mastered in a few minutes. I spent some time puzzling about the difference between data that's passed around by value (struct) and data that's passed around by pointers (class). Juggling these differences is one of the big challenges to C programming, and it will remain an issue for Swift programmers who want to squeeze the best performance out of their code.

Optimization will probably be a real headache for some kinds of code. Strings, for instance, are always passed by value, which means Swift will be making copies of strings left and right with every function call. This may not be much of an issue for graphics-based programs like games, but programs like Web servers and content management systems are essentially concatenation machines. Anyone who starts juggling strings will need to pay attention to this when writing Swift code. They may end up wrapping the string values in classes to avoid the copying.

There are a number of other areas that I earmarked for deeper study. Downcasting, for instance, lets you shift the focus from a class to a subclass with the typecasting operator (as). This may fail, so there's another typecasting operator with a question mark (as?), which returns the value of nil if the downcast doesn't succeed. There are a number of these optional features where the presence or absence of the question mark changes the behavior of keywords. Another feature, subscripts, lets you dig into a class quickly to pull out a particular value. It's neat shorthand, but it has the potential to confuse.

The Playground evaluates your Swift code as you type it. You type code on the left while it describes what's happening internally on the right.

A major commitmentThe simplicity of the Swift language is deceptive. While it's easy to write a "hello world" app in one line and build a quick app with a few lines of code, numerous details and nuances will take plenty of study to master. The adept programmers will love the power and the ability to use the clever tricks in their software, but many neophytes will find it hard to read their code. This will not be an easy language to master, no matter how easy it might be to pick up.

The most important thing to recognize about Swift is the scope. While some of new languages have a toylike feel because they're relatively small projects, Swift is a major commitment. The first book on Swift alone is excellent, in part because it's more than 850 pages long. The Xcode download now pushes 2.5GB. The language isn't a little playpen for easy coding, but a full software package designed to offer all of the support a team of programmers needs to take on major projects.

The language also includes copious hooks and tools for working with the existing Cocoa libraries. Apple doesn't want to rewrite the iOS and OS X stacks, so Swift makes linking to them simple. This entire project was built to maintain the interest in these stacks and to make it easier for anyone to walk in and start coding up apps. At the same time, you can use as much or as little Swift code in your iOS or OS X project as you like.

Apple isn't forcing a march to Swift. No doubt many of the old guard will stick with Objective-C. But Swift will make it much easier for programmers steeped in other languages and traditions to pick up Xcode and quickly become productive. That alone is a big step forward for Apple and one that will probably lead to even more native iOS and OS X code than ever. Swift doesn't need to take over the world to be a gift to it.

This article, "First look: Apple's Swift is simple, at first," was originally published at InfoWorld.com. Follow the latest developments in application development and mobile technology at InfoWorld.com. For the latest business technology news, follow InfoWorld.com on Twitter.

Read more about application development in InfoWorld's Application Development Channel.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

Tags MicrosoftGoogleAppleiosoperating systemssoftwareMac OS Xobjectiveapplication developmentmobile technologyDevelopment toolsMobile Development

More about AppleAustralian Pharmaceutical IndustriesGoogleMicrosoftTechnology

Show Comments
[]