Swift programming language has been announced and released on XCode 6 Beta for over 2 months. Although it is not out of the Beta version yet, we (the iOS mobile Developers) could get our hands dirty on the playing with the Swift code to understand the differences between the Swift and Objective-C Programming Language.
Let’s us make some comparisons between Swift and Objective-C and understand the differences between these 2 programming languages. We can also identify the strengths of Swift over Objective-C.
Swift Programming Language and Its Development
Swift Programming Language is been developed by Chris Lattner and his team since year 2010. He is the director of the Apple Developer Tools such as Xcode, the Instruments and the compiler. As of year 2014, it has been in the development for 5 years.
Steve Jobs passed away on late 2011. The development of Swift has started more than 1 year before Steve Jobs’s passing. It means that Steve Jobs could have involved in part of the Swift Language Development. There is a chance that it is Steve Jobs’s vision that a new and powerful programming language is needed to replace Objective-C (a 30 years old programming language). Only the internal development team could verify if this is a fact or just a random guess.
From watching the WWDC videos on Swift, we understand that Swift is designed to be more
– Safe
– Modern
– Power
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. For example, there is no need of semicolon “;” at the end of each line of code. A few powerful features such as Extensions have been introduced on Swift to ease the developer to create great apps in less time.
The Differences of Swift and Objective-C Code Syntax
Let’s compare the differences between Swift and Objective-C Programming Language. Let’s learn what makes Swift a better programming language.
I have used NSDictionary, NSMutableDictionary, NSArray and NSMutableArray quite extensively on most of my iOS project. So, let’s start to compare these syntax.
Dictionary and Array in Objective-C versis Swift
Objective-C Code
//Non-Mutable Dictionary
NSDictionary * myFixedDictionary = @{@”key1″:@”This is value1″,@”key2″:@”This is value2″};
// Mutable Dictionary
NSMutableDictionary * myFlexibleDictionary = [[NSMutableDictionary alloc]init];
//Set Object using Old Syntax
[myFlexibleDictionary setObject:@”This is value1″ forKey:@”key1″];
//Set Object using New Syntax
[myFlexibleDictionary setObject:@”This is value2″ forKey:@”key2″];
NSLog(@”myFixedDictionary: %@”,myFixedDictionary);
NSLog(@”myFlexibleDictionary: %@”,myFlexibleDictionary);
//Non-Mutable Array
NSArray * myFixedArray = [[NSArray alloc]initWithObjects:@”Object1″, @”Object2″,nil];
//Mutable Array
NSMutableArray * myFlexibleArray = [[NSMutableArray alloc]init];
//Add Object using Old Syntax
[myFlexibleArray addObject:@”Object1″];
//Add Object using New Syntax
myFlexibleArray[1] = @”Object2″;
NSLog(@”myFixedArray: %@”,myFixedArray);
NSLog(@”myFlexibleArray: %@”,myFlexibleArray);
Swift Code
//Constant Dictionary (Almost similar with Non-Mutable Dictionary)
let myFixedDictionary = [“key1″:”This is the value1″,”key2″:”This is value2”]
//Variable Dictionary (Almost similar with Mutable Dictionary)
var myFlexibleDictionary = [String : String]()
myFlexibleDictionary[“key1”] = “This is the value1”
myFlexibleDictionary[“key2”] = “This is the value2”
println(“myFixedDictionary: \(myFixedDictionary)”)
println(“myFlexibleDictionary: \(myFlexibleDictionary)”)
//Constant Array (Almost similar with Non-Mutable Array)
let myFixedArray: [String] = [“Object1”, “Object1”]
//Variable Array (Almost similar with Mutable Array)
var myFlexibleArray = [String]()
myFlexibleArray.append(“Object1”)
myFlexibleArray.append(“Object2”)
println(“myFixedArray: \(myFixedArray)”)
println(“myFlexibleArray: \(myFlexibleArray)”)
From the above Objective-C and Swift codes are doing exactly the same thing for Dictionary and Array declaration and initialisation. With just the First glance, I believe that you will realize that the code for Swift is simpler and also short.
Check Out the complete documentation for Swift Programming Language: Swift Programming Documentation by Apple.
var accessories: [HMAccessory] = []
how can i write about statement in objective c?
Hi,
It should be something like:-
NSMutableArray * accessoriesArray = [[NSMutableArray alloc]init];
HMAccessory * hmaAcessory1 = [[HMAccessory alloc]init];
HMAccessory * hmaAcessory2 = [[HMAccessory alloc]init];
[accessoriesArray addObject:hmaAcessory1];
[accessoriesArray addObject:hmaAcessory2];
You used the quick array implementation in swift but not in objc? @[]
Thank you for sharing your article. This is very informative article to Swift and Objective C programming languages.
Keep it up.