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!