Apple’s Swift: A Developer’s First Impression

Our developers Jordan Clifton and David Sweetman dove into the new programming language, “Swift,” that was announced yesterday during Apple’s keynote at WWDC14. After hearing the announcement and viewing the demo we were excited to jump in and start learning more about Swift using the Playground. Playgrounds are used to experiment with new technologies, analyze problems, and prototype user interfaces.

Developers watching WWDC14 in the new office.

Swift uses the same runtime as Objective-C under the hood, and also uses Automatic Reference Counting (ARC); in fact, in the book “The Swift Programming Language,” Apple suggests that a major motivation of ARC was to pave the way for Swift. Swift also offers seamless access to existing Cocoa frameworks and is fully interoperable with existing Objective-C code.

Here are some comparisons between equivalent operations in Objective-C and Swift:

Array initialization:

Objective-C

NSArray *myArray = [[NSArray alloc] init];

Swift

let myArray = String[]()

Both lines of code create an empty array using the initializer syntax.

Another exciting element of Swift is how clean and easy it is to read the syntax. This allows developers to produce more maintainable code while having fun doing so. Additionally, Swift includes a for-in loop that makes it easy to iterate over arrays, dictionaries, ranges, strings, and other sequences. The loop can use exclusive and inclusive ranges, and parentheses are optional. Traditional for loops are also available to use as well.

Using the for-in loop in Swift would look something like this:

for index in 1...5 {
println("index: \(index)")
}

This will loop through numbers 1-5 printing what the current index is. This syntax is another way Swift has successfully increased readability making it very easy to understand what the code does. At first glance, Swift seems to bring a nice mix of syntax and features to Apple development that will likely help write concise code and express instructions in ways that that were not previously possible.

One of the most exciting parts of Swift is its functions and methods. In Swift, functions are first-class types. Compare that to Objective-C, where you have methods which must belong to a class. If you want to create a function outside of a class in Objective-C, you have to use C functions or blocks.

The below example demonstrates a class that defines a simple class/type method to add two integers. Notice that Swift obviates the need for a separate interface:

The Swift type method in the MyClass declaration above would be called like this:

var value = MyClass.addValue(5, toValue:4)
// value = 9

Calling the method highlights an interesting naming idiom suggested by Apple: in methods, the first parameter is not assigned an external name automatically, but subsequent parameters are, so we pass it the parameters as `5, toValue:4`. `toValue` is the external name of the second argument, and is included for clarity, to give the same readability that Objective-C does by interspersing arguments in the method name. The first parameter “5″ does not need an external name, because the method name itself sufficiently describes the first variable.

It looks like things will get pretty fun with functions in Swift: currying, variadic arguments, multiple return values. Definitely take a look at the “Functions” and “Methods” sections of the Swift Programming Guide to learn more of the details.

This article is simply a brief look at how Swift might change the face of iOS development. So, if you're a beginner should you still learn Objective-C? Our thoughts here.