WHAT’S SWIFT? WHY SWIFT?

Peter Wood in Swift
01 Jul 2014, 14:41

Hi there! Peter Wood from Eltima is here again.

On the 4th of June 2014, Apple Inc. held its annual developer conference WWDC 2014. Apart from giving a glimpse into the future of OS X and iOS 8, Apple announced Swift – a brand-new programming language. That’s the thing I would like now to muse upon.

Swift is a young programming language which will be on its active development stage for a quite long period of time. It will be gradually enhanced and brought to perfection.

For example, the current version does not feature access modifiers. But they are due to appear soon:
“We don’t usually promise anything for the future, but in this case we are making an exception. Swift will have access control mechanisms”.

That’s certainly good news! You are welcome to take a tour of the new language and its features with the book The Swift Programming Language.

Today, I’ll give only the highlights of this event.

Explicit Optionals

var a:String
var b:String?
a - this is a string and it always contains some value
b - this string either contains a value or contains nothing

Such approach permits to avoid ambiguity present in Objective-C when calling method from nil or passing nil as argument.

For example:
method [NSString stringByAppendingString:]

What if we pass nil as an argument? I’ll give you a hint while you are studying the doc. Optional come to our aid!

Here’s the Swift example:
func stringByAppendingString(aString: String) → String

Actually, since NSString is Objective-C class, this method prototype will look somewhat different, but that is a long story to tell. Let’s discuss implicitly unwrapped optionals a bit later.

Here’s the example of passing nil as an argument:
func stringByAppendingString(aString: String?) → String

Thus, all the limitations applied to the argument at the method calling become obvious, even without looking into the docs.

Type Inference

That’s easy as snap. Let’s compare:

Objective-C variant:
NSString *stringConst = @”string”

and Swift variant:
let stringConts = “string”

Saving developers tons of typing, that’s really cool. More comfortable and readable. But don’t forget – that’s only a statically-typed variable, but not the id analogue. The compiler infers types automatically and you can omit type annotations. Though if you want, type annotations can still be applied.

Example:
let stringConst:NSString = “string”

Generics

Generics is the most stunning Swift innovation. It permits to write flexible and reusable code whilst saving static typing, alike C++ templates.

Example:
var array:Array<String> = [”first”, “second”]
println(array[0].uppercaseString)

Functions with multiple return values

Sick and tired of pointers to pointers in method arguments and their subsequent checking? Then functions with multiple return values are created for you!

Many Objective-C methods use NSError and NSError ** pattern (pointer to NSError pointer) to catch errors which looks slightly outdated nowadays.

So what would +stringWithContentsOfFile:encoding:error: method look like without using such pattern? That’s how:
class func stringWithContentsOfFile(path:String, encoding UInt) → (string:String?, error:NSError?)

To sum up

Nobody knows what is the future of Swift. Will it suffer the same fate as Garbage Collection in Objective-C, or will Objective-C suffer the same fate as Carbon API? Or will Objective-C and Swift coexist happily? Only time will tell.

Swift features some crucial options we really missed in Objective-C development. With Swift, many things became much easier now.
That’s why we definitely decided upon Swift for our future project. Promise to share real examples from Swift development with you.

Stay tuned,
Peter Wood from Eltima Software.