CIO

10 Things You Should Know About Apple's Swift

Apple has introduced Swift as the new language for OS X and iOS app development

Apple recently unveiled Swift, a new language to replace Objective-C for OS X and iOS application development. Apple won't accept submissions built using Swift to the iOS or Mac App Store until the fall, when iOS 8 and the next version of OS X (Yosemite) ship, so there's still some time to learn the ins and outs of this new programming language.

Without further ado, here are 10 things you need to know about Swift.

Swift should appeal to younger programmers. Swift is more similar to languages such as Ruby and Python than is Objective-C. For example, it's not necessary to end statements with a semicolon in Swift, just like in Python. In Objective-C, on the other hand, it's necessary to do so; forgetting a semicolon at the end of just a single statement can cause errors. If you cut your programming teeth on Ruby and Python, Swift should appeal to you.

[ Feature: 8 Developer Picks for the Best Starter Programming Language ]

That said, Swift is compatible with existing Objective-C libraries. There's no problem with writing new modules in Swift that interoperate with existing Objective-C code bases. That may make Swift attractive if you've already built a considerable skill base in Objective-C, too.

Swift should be a safe(r) language. Apple has made an effort to make Swift safe in a variety of subtle ways. For starters, programmers must include brace brackets to open and close "If" statements, which prevents bugs such as the SSL "goto fail" error. In addition, switch statements must include a default statement. This guarantees that something will run at the end of the statement even if none of the possibilities in the statement are satisfied.

Swift isn't that fast. Despite the name, Swift is unlikely to result in applications that run much faster than applications written in Objective-C. Although the two languages are different, they're not that different both target the same Cocoa and Cocoa Touch APIs (for OS X and iOS, respectively), both are statically typed languages and both use the same LLVM compiler as well. There will inevitably be performance differences, as the two languages aren't identical after all, but don't expect significant differences.

Swift is incomplete. The language that's available today isn't the finished product. Apple is still working on it, and it's highly likely that new features will be added over the coming months. While it may well be worth coding in Swift to familiarize yourself with the language, to do so you'll need to use Xcode 6 beta and the iOS 8 SDK (also in beta). And don't forget: Apple's app stores won't accept apps built with Swift until it first releases Yosemite and iOS 8.

[ Apple: Nearly 80 Percent of Macs Will Be Able to Run OS X Yosemite ]

You can experiment with Swift code in "Playgrounds." One of Swift's most interesting features is an interactive environment called a Playground. This tool lets you see the effects of changes or additions to code as you type, without going through the time-consuming rigmarole of running the code through the compiler and executing it.

Other Playground features include the capability to "watch" the value of a variable, typing its name on a separate line in the code and seeing its current value displayed in a side bar, as well as a set of "Quick Look" buttons that display images, strings and other content intended for graphical display.

Swift offers type inference. Like Scala, Opa and other programming languages on the rise, Swift carries out type inference. Coders don't need to spend time annotating variables with type information and risk making mistakes; in most cases, the compiler can infer the type from the value that a variable is being set with.

[ More: 6 Emerging Programming Languages Career-Minded Developers Should Learn ]

As a result, you can expect to find fewer type-related bugs hiding in your code. Plus, thanks to smart optimizations, your code should run faster.

Swift introduces Generics. In static typing, when you write a function, you have to declare the types of the function's parameters. That's fine until you have a function that you want to work in different circumstances with different types.

[ Feature: 10 Programming Features Apple Swift 'Borrows' From Other Languages ]

Enter Generics. Much like Templates in C++, Generics are functions that can be reused with different variable types without being rewritten for each type. For example, a function that adds up the contents of an array. In some cases, the contents might be integers; in other cases, floating point numbers.

Swift handles strings more easily. If string handling drives you mad in Objective-C, then you'll love Swift, as the way you deal with strings in the new language is much simpler. Most notably, you can concatenate strings easily using "+=" and compare strings using "==" instead of the more cumbersome "isEqualToString:". Strings can also be used in switch statements.

Swift tuples offer compound variables. A tuple lets you group multiple elements into a single compound variable. The values in a Swift tuple can be of any type and don't have to be the same type as each other. You can make a tuple from any permutation of types that you like: (Int, Int, Int) or (int, String) or (String,Bool) or whatever else you need.

There are a number of ways to get the values in a tuple. You can access them by index number (starting with 0), for example, or you can decompose a tuple into separate constants or variables.

Apple is the master of Swift. Having been around for 30 years, Objective-C is getting rather long in the tooth. Nonetheless, Apple didn't articulate a precise reason for introducing the new language. The most likely reason? As the creator of Swift, Apple is free to add or change any functions that it wants, whenever it wants.

There's also the advantage that, once Swift becomes mainstream, it will make porting iOS apps to Android that much harder. You won't be able to use existing and relatively mature tools that port Objective-C to Java.