About Swift – A Next Generation Programming Language by Apple


What is Swift?

Swift is a new programming language created by Apple for iOS and OS X development. Introduced at Apple’s 2014 Worldwide Developers Conference, Swift is designed to work with Apple’s Cocoa and Cocoa Touch frameworks and the large body of existing Objective-C code written for Apple products.


Why should I learn Swift?

“Swift” will be the next generation programming language for iOS and OS X platform. Or you can consider it as a replacement  of 30 years old programming language “Objective-C”.

⇒ Swift is designed to be more:

– Productive
– Modern
– Safe
– Easy to Learn

The syntax for Swift is designed so that the programmer/developer could make less erroneous codes. When there is any problematic code, the error/warning inside the text editor of the XCode will be much clearer that forces the programmer to correct the mistake.

When we program using Swift, we will also write lesser lines of codes when comparing with Objective-C.


Differences between Swift and Objective-C ?

⇒ Swift is more type safe

Swift is less prone to errors because in Swift, things like arrays and dictionaries need to have a class type specified and they can then only accept objects of that type. In Objective-C, you were able to put a mixture of different object types into a collection like an array or dictionary.

⇒ The syntax is more natural and concise

While it’s a hassle to learn new programming language, the syntax of Swift is actually easier to read and understand.

For instance, to declare a variable you’d write:

var myVariable = 15
var myVariable = “Hello World”

Similarly for defining a return type of a method, you use an arrow like this:

func getDifference(firstNumber:int, secondNumber:end) -> int 
  return firstNumber - secondNumber;

println(getDifference(9, 5));

Following piece of code differentiate the syntax between Objective-C and Swift:

Objective-C Code:

NSDictionary* fixedDictionary = @{@"name":@"Raja Khan",@"country":@"United States"};
NSMutableDictionary* mutableDictionary = [[NSMutableDictionary alloc]init];
[mutableDictionary setObject:@"This is Value-1" forKey:@"Key-1"];
[mutableDictionary setObject:@"This is Value-2" forKey:@"Key-2"];
NSLog(@"myFixedDictionary: %@", fixedDictionary);
NSLog(@"myFlexibleDictionary: %@", mutableDictionary);

NSArray* fixedArray = [[NSArray alloc] initWithObjects:@"Object-1", @"Object-2", nil];
NSMutableArray * mutableArray = [[NSMutableArray alloc] init];

[mutableArray addObject:@"Object-1"];
mutableArray[1] = @"Object-2";

NSLog(@"fixedArray: %@", fixedArray);
NSLog(@"mutableArray: %@", mutableArray);

Swift Code:

let fixedDictionary = ["Name" : "Raja Khan", "Country" : "United States"]
var mutableDictionary = [String : String]()

mutableDictionary["Name"] = "Raja Khan"
mutableDictionary["Country"] = "United States"

println("fixedDictionary: \(fixedDictionary)")
println("mutableDictionary: \(mutableDictionary)")

let fixedArray: [String] = ["Object-1", "Object-1"]
var mutableArray = [String]()


println("fixedArray: \(fixedArray)")
println("mutableArray: \(mutableArray)")


⇒ No more semi colons

To make Swift more easier for non-programmers, Apple decided to remove semi-colons.

You don’t end your statements with semi-colons, instead you start a new line.


⇒ No More Header and Implementation Files

A Swift doesn’t require any separate header and implementation file.


Check Out the complete documentation for Swift Programming Language:  Swift Programming Documentation by Apple.