Apple's New Language - Swift
Written by Mike James   
Tuesday, 03 June 2014

This year's WWDC has been dominated by software announcements rather than the cool hardware that most Apple fans were eagerly predicting and anticipating. The most important is the new Swift language.

swifticon

 

At the moment it is difficult to know what intentions Apple has about popularizing its new language outside of the iOS/OSX arena. My guess is none at all. I also doubt very much if it will be open sourced or submitted to a standards programs. Apple really only needs Swift to be a success in its own backyard.

How best to describe Swift?

I guess the cruel way to put it is - nothing special - but it doesn't need to be. It does incorporate some of the best recent ideas from languages such as C# and even JavaScript.

The most important thing about it is that it looks easy. You can look at some Swift code and think you were looking at almost any modern language and, compared to Objective C which it is intended to replace, it is simplicity itself - baby talk rather than rocket formulas. 

This is the sense that some people are referring to it as a "scripting language". The idea of a scripting language is a very vague one but it seems to mean something along the lines of "easy enough for the non-expert" to use. I'm not sure that Swift drops the bar quite that low but, as already suggested, it is a huge improvement over Objective C.

Just in case you think that calling it a scripting language implies that it is interpreted and hence slow, it is worth adding that It is based on the LLVM compiler project and the code that is produced is very fast. Apple claims that the same code runs faster in Swift than Objective-C and a lot faster than Python - between 4 and 200 times faster in fact. 

So what is Swift like?

Swift is a statically typed language, but it uses type inference so you don't have to declare the type of everything you use.

One of its most interesting features is the use of optionals - variables that can have a null value. If a variable is an optional type then its value is either true or false depending on whether it is defined or not. To get its underlying value you have to use a forced unwrap operator !.  If you use the unwrap operator and the variable doesn't have a value then you get a runtime error. This means that you end up writing things like:

if possibleNumber { println( possibleNumber! )}

You can't generate a runtime error by using a null reference because variables always have a value; but you can if you try and unwrap a variable you haven't tested. 

The usual data structures are provided - strings, arrays, tuples and a dictionary type. You also get the usual control statements. The for loop can make use of closed ranges and half open ranges, i.e. 0...10 is 0 to 10 inclusive and 0..10 is 0 to 9. The switch statement is particularly nice - no fall through and comparatively sophisticated pattern matching on the case expressions. 

On a higher level, the language has single inheritance class-based objects. It has autogenerated getters and setters and a lot of the more modern facilities such as extension methods, generics and protocols. Protocols are essentially what other languages call interfaces and they serve the same purpose - to allow a safe and limited form of multiple inheritance. 

xcodeswift

 

Functions in Swift are first class in the sense that you can pass them as parameters and as return values, but they are not objects. They do support closure and closure expressions are essentially lambda expressions. 

There are lots of small details I haven't described and you can read the full language specification as a pdf to find out more. The main conclusion is that this is a nice clean language that has been formulated with the advantage of seeing what mistakes other languages have made. How it evolves as it accreted additional features will be interesting to see.

Apart from the change in terminology - why does Apple have to call so many things by different names - if you know a modern language then you will recognize a lot of Swift and if you know more than one modern language you will recognize it all!

There is a second part to the Swift story, however -  a language is not enough. 

When you create programs for iOS or OSX it is the Cocoa class library that will cause you as much trouble as the language. Today what matters is how easy it is to use a language and its class library. In this case the problem is difficult because Cocoa is written in Objective-C and is likely to remain written in that language for the foreseeable future. 

The good news is that Swift interoperates with Objective-C and the there are wrappers for the Cocoa library.

To give you some idea what the change looks like here is an Objective-C object instantiation:

 

UITableView *myTableView = [[UITableView alloc]
             initWithFrame:CGRectZero
                    style:UITableViewStyleGrouped];

and here is the equivalent Swift:

 

let myTableView = UITableView(
             frame: CGRectZero, style: .Grouped)

 

You can see that it appears to be much simpler and that's the point. 

Swift does appear simpler than Objective-C. What we really need to know is if it is simple enough to encourage all those casual programmers and programmers who know Java, JavaScript and C# to give iOS/OS X apps a try. 

This is the reason Apple invented Swift and it is the reason why there is a tiny chance that the language might break out of the backyard and make it onto other devices. If Apple could say "learn Swift you can use it lots of places" then it might make it even more attractive. 

I doubt this will happen. 

Apple has made available online documentation and a free ebook on iBooks Store. You can also download the beta release of XCode 6 and get started with using the language. 

 swifticon

More Information

 Welcome to Swift

 XCode 6

Related Articles

I TOLD You Gotos Are Dangerous!

AppCode 3.0        

Apple's New Phones - The Programmer's Take       

Apple Goes Flat - Where's The Logic In That?       

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

Banner


Google Releases Gemini 2 And Jules Code Agent
18/12/2024

Google has announced an updated version of Gemini, saying that Gemini 2.0 Flash Experimental will "enable even more immersive and interactive applications", along with new coding agents that can take  [ ... ]



Hour Of Code 2024 Is About To Kick Off
04/12/2024

This year the event that aims to provide a coding experience for all school students and anyone else who wants to join in runs between December 9th and 15th and includes new activities. Let's find out [ ... ]


More News

 

espbook

 

Comments




or email your comment to: comments@i-programmer.info

 

Last Updated ( Tuesday, 03 June 2014 )