• Skip to main content
  • Skip to primary sidebar
  • Home
  • About
  • My Portfolio
  • Contact

mobileoop

I am a Senior iOS developer based on Singapore. Developing iOS Mobile apps for about 8 years now.

  • Home
  • About
  • Portfolio
  • Contact Me

The Comparison Between Swift and Objective-C Programming Language

August 11, 2014 by Ricky 4 Comments

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.

Filed Under: iOS, Objective-C, Swift

Reader Interactions

Comments

  1. checkmate says

    January 21, 2015 at 5:54 am

    var accessories: [HMAccessory] = []
    how can i write about statement in objective c?

    Reply
    • Ricky says

      January 21, 2015 at 8:15 am

      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];

      Reply
  2. P says

    April 20, 2015 at 11:07 pm

    You used the quick array implementation in swift but not in objc? @[]

    Reply
  3. iprism says

    July 7, 2017 at 1:57 pm

    Thank you for sharing your article. This is very informative article to Swift and Objective C programming languages.
    Keep it up.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Search This Website

About Me

profile for Ricky at Stack Overflow, Q&A for professional and enthusiast programmers
I have been developing iOS mobile apps for about 8 years now. Currently, I am working as a Senior iOS Developer at a company based in Singapore.

mobileoop.com is a website that I share my thoughts on the latest technology particularly on the mobile apps and its ecosystems. Sometimes, I also write my experience in working on various iOS projects (my online portfolio).

This is my Github profile.

You may connect to me via LinkedIn: My LinkedIn.

Recent Posts

  • MVVM and RxSwift – iOS Apps Development
  • hotspot – Mobile Apps for Singapore Tourists
  • Nestia – Lifestyle Mobile Apps in Singapore
  • Watch Over Me Version 6.0
  • Chan Wu – Audio and Video Freelance iOS App Project
  • Getting Location Updates for iOS 7 and 8 when the App is Killed/ Terminated/ Suspended
  • Auto Layout Advanced Techniques for iOS 8 and 7 using XCode 6 on Storyboard
  • How to Use Auto Layout in XCode 6 for iOS 7 and 8 Development
  • The Comparison Between Swift and Objective-C Programming Language
  • Swift, Home Automation and Indoor Positioning System

Copyright © 2023 mobileoop.com