Clarifying Swift & Objective-C

swiftApple made a surprising announcement at the recent WWDC held between June 2-6 2014. While everyone was looking forward to the launch  of the new OS for both the Mobile as well as Mac platforms, Apple also announced a new programming language for developers. SWIFT.

It was described as Objective-C without C. Over the past few months I have gone over the features of the language & even converted many of the apps into Objective-C. While the initial learning curve is there, it is a fairly easy language to pick up. I will be going over a few interesting features of the language in this article. But what I will also be touching upon is the future & relevance of Objective-C.

Reduction in Lines of code: One of the first things that you will notice once you start programming in Swift is the reduction in the number of lines of code. Swift does away with the long syntax which you find in Objective-C. This makes the code look a lot more compact & less intimidating.

Lets take the example of creating an object in Swift & compare the syntax with that of Objective-C

Objective – C version: NSString *myString = [[NSString alloc] initWithString:@”This is a string”];

Swift version: var myString : String = String(“This is a string”)

As we can see, there is a great deal of simplicity that comes along. Reduction in the number of lines of code is of great importance to software developers. This means more compact code & improves readability. However, this comes at a price. While Objective-C code was more elaborate & did use many more lines of code it was far easier to understand by simply reading through it. In case of Swift the code will have to be complemented with details comments about what it does.

Single File: Another new feature there in Swift is the lack of header files. People coming from a Java background would find this familiar. The declaration of classes & implementation of its functions is done in .swift file now. This removes the necessity of having to import various header files. In case, certain frameworks need to be used all that needs to be done is to import that framework at the top of the file.

import framework

Replace the keyword framework with the name of the framework you wish to include.

Mixed Language Programming: Unlike Objective-C, Swift does not work well with other languages. In fact, Swift only works with Objective-C. So if you have any API’s or pieces of code that you have written in C or C++, you will have to create Objective-C wrapper classes for that C/C++ code so that you can incorporate it into your project.

This means that Objective-C is not going to be deprecated any time soon. Also, it will be equally important to learn Objective-C & can’t be ignored by people.

COMMON QUESTIONS:

– I already know Objective-C should I also learn Swift?
There is no pressing need to immediately learn Swift, though it would be a good idea to learn it anyways. None of the apps you have already made will stop working. However, you may come across code written in Swift or would need to incorporate programs written in Swift, so having knowledge of the same would be a good idea.

– I don’t know Swift or Objective-C which should I learn?
Again, as in the previous question knowing both is a really good idea. If you are confident that you will be making apps using Swift only, then you can learn just that for starters. If you feel that there is a good chance of having to read/modify or write Objective-C code then learning Objective-C before Swift makes more sense.

CONCLUSION: Swift is a very easy to use language & will sit nicely on familiar eyes. A lot of the code that will be written in the coming apps will definitely use Swift. However, this does not mean the end of the road for Objective-C. Nor does it mean that Objective-C will become redundant or obsolete. A developer can choose to use any of the languages to make the app. It will be nice for a new developer to first learn Objective-C & then move on to Swift as it will give the user a more all round understanding of the entire development framework .

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s