Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

10 things you should know about Apple's Swift

Paul Rubens | July 22, 2014
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.

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.

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.

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.


Previous Page  1  2 

Sign up for CIO Asia eNewsletters.