Programming, Technology


At Apple’s 2014 World Wide Developers Conference (WWDC) a new programming language called Swift was introduced. Big deal you say? Well, for most Apple developers (both full-time and part-time – (like me)) it was a shock. There had been no rumours, which for an Apple release was unusual. We thought Objective-C would live forever! But the king was dead – long live the king. For me, Objective-C had played a large part in helping me to learn to programme. It’s quite ‘English-like’ in it’s syntax, which means you can generally tell what a piece of code is doing. For example:

NSString *myText = @"Hello World";

It’s fairly clear what this line of code is doing – you are assigning the words ‘Hello World’ to a variable called ‘myText’ (forget about the NSString[1] thing for now). Easy hey? Now here’s how to do it in Swift:

var myText = "Hello World"

Even easier! There’s no need to reference the class of object (NSString) because Swift knows that ‘Hello World’ is a string of characters (hence the class name). This is called type inference, and it’s a big thing. You can do this with other data types as well like numbers. Assigning a number to a variable in Objective-C and then Swift involves:

int myNumber = 5;   //Objective-C
var myNumber = 5   //Swift

Again, Swift knows that 5 is a whole number and it’s therefore an integer (int), so there’s no need for you as the programmer to inform the computer about this. To assign a floating point number involves:

float myNumber = 5.2;   //Objective-C
var myNumber = 5.2   //Swift

Swift also makes it really easy to combine strings together, like:

var string1 = "Jim"   
var string2 = " You're great!"   
var combinedString = string1 + string2

It’s a little more complicated in Objective-C:

NSString *string1 = @"Jim";
NSString *string2 = @" You're great!";
NSString *combinedString = [string1 stringByAppendingString:string2];

Incorporating data into text is easy in Swift too:

let num1 = 6
let num2 = 4

if num1 > num2 {
    println("\(num1) is greater than \(num2)")
} else {
    println("\(num1) is not greater than \(num2)")

The equivalent in Objective-C:

int num1 = 6;
int num2 = 4;
if (num1 > num2) {
     NSLog(@"%i is greater than %i", num1, num2);
} else {
     NSLog(@"%i is not greater than %i", num1, num2);

Not a lot of difference there, but I think the Swift version is a little easier to read.

Now, arrays. An array is a collection of objects. For example, a collection of numbers. In Objective-C it’s this:

NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject:[NSNumber numberWithInt: 2]];
[myArray addObject:[NSNumber numberWithInt: 4]];
[myArray addObject:[NSNumber numberWithInt: 6]];
[myArray addObject:[NSNumber numberWithInt: 8]];

In Swift it’s just this:

var myArray = [2, 4, 6, 8]

Yep, that’s all there is to it! One of the problems with learning to programme is that people think it’s too hard. They say “I’m not smart enough to do this”. Well, if I can learn it then anyone can! That being said, there are quite a few ‘gotchas’ with Objective-C, and aspects of the language that are difficult to grasp. I think Swift is easier to comprehend than Objective-C and therefore easier to learn, so I’m confident that Swift will enable more people[2] to pick up programming for iOS and the Mac, and that’s a good thing.


  1. This is what’s called a ‘class’. A class is essentially a template for making things. In this case it’s a template for making ‘string’ objects, which in everyday language are words. I’m guessing the word string comes from a ‘string of characters’, which is what a word is. When you want to make a new string, you ask the class to give you one. You can then do things with that string, like assign it to a label that appears on screen. The ‘NS’ is a reference to NeXTSTEP, which was an operating system created by NeXT Computer, a company that Steve Jobs started after he was kicked out of Apple in the mid 1980’s. NeXT Computer, and the NeXTSTEP operating system were bought by Apple in 1997 when Jobs returned to Apple and were eventually turned into OSX and iOS.  ↩
  2. On that note, I’d encourage any university student to have a crack at learning a language. A number of my PhD students have started doing so and it has allowed them to analyse data in new ways that they couldn’t do with off-the-shelf apps.  ↩