Since most of the readers and students of the course started their iOS development journey with Objective-C, I’m writing this article series to introduce the Swift programming language by showing Objective-C code snippets and their Swift equivalents.
In the next few articles, we’ll start comparing things like variables, classes, methods and properties between the two languages but in this first article, I’m going to introduce some broader differences that you’ll notice as you begin learning Swift with an Objective C background.
It’s important to remember that the Swift programming language is still under development which means that things can change.
With that said, here are some broad differences which struck me right away when I was learning Swift.
Swift is much more type safe
I think this is what Apple meant when they said that 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.
This type checking in Swift makes it so that you can be confident that a collection will only contain objects of the specified type rather than having to check if the object is a certain class type before operating on it as you sometimes have to do with Objective-C.
The syntax is more natural and concise
While it’s a hassle to learn a new programming language, the syntax of Swift is actually easier to read and understand. That was my first impression while I was learning the Swift syntax.
Or maybe it’s just that Objective-C is very verbose.
For instance, to declare a variable, you’d write
var myVariable = 10
var myVariable = “some text”
Which is a lot easier to understand for a non-programmer.
Similarly for defining the return type of a method, you use an arrow like this:
func myMethod -> Int
As you’ll see in the next few articles when we do some comparisons, Swift is a joy to read.
No more semi colons
While it was natural for me to write semi-colons at the end of my statements because of other programming languages that did the same, I soon realized that non-programmers found it a hassle to remember to do so!
In Swift, you don’t end your statements with a semi colon; instead you start a new line.
No More Header and Implementation Files
In Swift, a class is pretty self contained and doesn’t require a separate header and implementation file.
It’s not particularly difficult to understand once it “clicks” for a beginner but I can recall many times when I helped people with the differences and reasoning behind having a .h and .m file and where to declare certain things.
With my short time using Swift so far, I can see why Apple said that this programming language is more beginner friendly. I don’t have anything bad to say about it yet because I haven’t spent enough time with it nor have I actually built much with it. However, I’m keeping in mind that it’s still a work in progress and I’m optimistic for it catching on with developers.
Let’s move on to part 1 and explore the differences with variables, classes, methods and properties in Swift!
This may be a stupid question, but I’m going to ask it anyways. Can I still put out my app in 2- 3 months using Objective C? I think the hardest part, for me so far, is I will have to get over the habit of not putting in semi colons.
For now, you dont have the choice if you wish to publish an app as Xcode 6, which is only compatible to compile the Swift language, doesn’t offer (yet) the possibility to create an app to submit to Apple.
It is there for people who want to learn and adapt.
It is to be expected that people with 5 years (or even less) of experience in Objective-C, won’t give up this language for Swift in one day.
Some blogs and dev notes state that Objective-C will still be around for a couple of years (unless Apple decide to kill it by forcing users to switch). Others mention that Swift is still kind of a Beta and that Apple is not done to make it better. (from what I heard, the language is good but not 100% perfect yet)
So, Objective-C isn’t a lost cause. I really believe the change will come naturally when we will start to see more Swift, we will start to mix both languages and find ourself in a confortable zone.
PS1: There are no stupid questions, only stupid answers.
PS2: In Swift, you can put as many semicolon as you want. var numberOfTables = 4; works perfectly! (In Swift, you dont have to, but you can! it is the same with ( ) in the if statements)
From my personal experience, not put semicolon is actually easier than put one. When I code Swift and after come back to Xcode 5, I get multiple errors because I left 3 or 4 semicolons behind.
Hope it helps
Yeah, that helped out a lot, thanks!