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.

1 2 3 Page
Recommended
Join the discussion
Be the first to comment on this article. Our Commenting Policies