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
var myArray = String[]()
Both lines of code create an empty array using the initializer syntax.
Creating an array with items also looks quite different:
Objective-C
NSArray *myArray = [[NSArray alloc] initWithObjects:@"one", @"two", @"three", nil];
Swift
var myArray = ["one", "two", "three"]
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:
Objective-C
@interface MyClass : NSObject
+ (INT) addValue:(INT)value1 toValue:(INT)value2;
@end
@implementation MyClass
+ (INT) addValue:(INT)value1 toValue:(INT)value2 {
return value1 + value2;
}
@end
Swift
class MyClass : NSObject {
class func addValue(value: INT, toValue: INT) -> INT {
return value + toValue
}
}
The above demonstrates basic Swift function syntax:
func functionName(firstArgName: arg-type, secondArgName: arg-type) -> return-type {
// code block
}
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.
We are Smashing Boxes. We don’t just build great products, we help build great companies. LET’S CHAT.