Apple has high hopes for Swift, a brand-new coding language it unveiled for its developers on Monday at its Worldwide Developers Conference. To Apple, Swift is a simpler, safer, faster-to-run alternative to the somewhat clunky and error prone language Objective-C now used to write apps for iPhones, iPads and Macs.
Essentially, Apple is wagering that Swift will save enough time and effort that it might encourage more developers try their hand at building iOS apps. So Apple would clearly like to see developers learn and adopt Swift—well, swiftly.
If only it were that easy.
With Swift, Apple is performing a complicated straddle between its current technology—the language and tools that developers are familiar with—and its shiny new language of the future. Swift certainly offers a number of attractive new features, including automatic memory management intended to kill off a class of insidious bugs that plague Objective-C, a “playground” feature that visualizes a program’s actions for easy debugging, and a simplified syntax designed to be both easier to learn and less prone to error.
But to maintain continuity with existing apps, tool suites and code libraries, Swift also snuggles up close to Objective-C. It works directly with Cocoa and Cocoa Touch, the Objective-C frameworks that drive OS X and iOS applications. Since the Cocoa libraries are implemented in Objective-C, developers can run Swift code and Objective C code side by side.
The trick here is that this kind of interoperability basically has to work perfectly for Swift to succeed. If it doesn’t, then developers will still need to tinker with Objective-C to make their apps work—and that means keeping up to speed in two languages, not one. Which is not the kind of chore developers tend to take on gladly.
They certainly didn’t the last time something like Swift came along.
The RubyMotion Experiment
According to Gregg Pollack, the founder and CEO of Code School, today’s programmers aren’t usually prepared for the complexity that is Objective-C. “Lots of developers started out learning Java, Ruby, and Python,” he said. “Moving from that to Objective-C is foreign, difficult, and full of ways for you to shoot yourself in the foot.”
Unfortunately, Objective-C was long the only language for building iOS and OS X apps. That’s largely because the Cocoa and Cocoa Touch APIs (see our API explainer) for Mac OS X and iOS are written in Objective-C, which means that developers usually write apps for Apple devices in the same language to maximize their performance.
But that’s not a hard-and-fast rule. You can use another programming language, so long as it can interface with the Objective-C code libraries in Cocoa and Cocoa Touch. So after seven years working for Apple, programmer and Ruby enthusiast Laurent Sansonetti started a new venture in 2012. Its mission? RubyMotion, an interface to make it possible to program iOS apps in Ruby.
As programming languages go, Ruby is well loved. It reads almost like English, with little unnecessary syntax. But RubyMotion never caught on.
“When things didn’t work, you’d still have to go a layer lower and tweak the Objective-C underneath,” Pollack said. “Now you had to know both Ruby and Objective-C—and the Cocoa framework—in order to build an iOS app effectively. So people said, ‘Why not just use Objective-C?’”
In some ways Swift is like an official Apple version of the RubyMotion experiment. It’s an easier language that interfaces directly with the Objective-C libraries and Cocoa and Cocoa Touch frameworks. But that means many—possibly all—Swift users are still going to need to know Objective-C. If that’s the case, why not just stick to what you know? That’s why Pollack doesn’t expect a sudden shift.
“There’s always a learning curve with new languages,” said Pollack. “It could take years for bigger companies to pick it up. They’ve got bigger apps they’ve worked on for years in Objective-C. They already have Objective-C developers. Why take the time to train them all in Swift, a language that hasn’t been proven yet?”
The Promise Of Swift
Of course, Swift has a few advantages RubyMotion could only dream of. RubyMotion was invented by a former developer; Swift is the favored child of Apple. RubyMotion was designed to work with Ruby on Rails and had to be tweaked for an iOS development framework; Swift, says Apple, provides “seamless compatibility” with Cocoa and Cocoa Touch.
It’s easy to sabotage yourself in Objective-C, which doesn’t always alert you to errors before testing a running app. If you create a variable and forget to assign it a value—easy enough to do—and then try to use that variable in a calculation, the resulting error can crash your app. Meanwhile, Swift will alert you ahead of time if you’re trying to use any variables that haven’t been assigned, saving you the time and headaches of trying to figure out why your program is crashing.
Swift also provides an advantage over Objective-C in its speed. Some Objective-C operations, like sorting and loops, have an inherent delay because of the number of functions that are called repeatedly under the hood. The compiler doesn’t know which of a dozen ways a function or object was used, so it tries many. Thanks to compiler optimization built exclusively for Swift, however, Apple says running Swift code is significantly faster than Objective-C.
That means that apps built with Swift could be faster and smoother for the end user, too. Jon Friskics, Code School’s iOS expert, predicted that users will notice increased speed in games and animations coded in Swift.
“The optimization won’t just make compiling faster and less prone to errors, but also the apps themselves,” he said. “Computationally intensive tasks like animations will likely run a little bit smoother.”
Of course, Swift is still a baby coding language. It almost certainly has its share of bugs (yes, computer languages have bugs, too). It’s likely going to go through a number of iterations and improvements while Apple and its beta testers fix them.
And Swift has one giant card up its sleeve that RubyMotion never had: It’s an Apple product, and that means Apple can change its APIs to suit the new language. If Swift does live up to its potential, it’s almost inevitable that Apple will eventually rewrite its Cocoa APIs in Swift, eliminating its primary dependence on Objective-C. If and when that happens, Swift will rapidly become the only Apple development language most programmers will ever need to learn.
Swift has the potential to make iOS development easier and, here’s hoping, enjoyable. At least that’s Pollack’s prediction:
Years from now, people will be able to just learn Swift and won’t have to learn Objective C. It’s going to take a lot of pain out of becoming an app developer. It could make it a lot more pleasurable to become an iOS developer—easier to learn, and more fun for people to program.