Learn Swift From Objective-C : Variables, Classes, Methods and Properties

In this article series, we’re going to look at some of the differences between Swift and Objective-C. This tutorial is best suited for people who already know Objective-C (at least the basics) and want to see what the equivalents are in Swift.

If you’re a non-programmer and you’re just beginning your journey into iOS development, I’d recommend taking a look at my course where you’ll learn Objective-C as part of learning how to build iPhone apps. When Swift is released, as a member of the course, you’ll have access to my new Swift course for free.

Ok, so lets get into it! This is a handy language guide to keep around as you’re learning Swift.

Variables and Constants

Swift makes declaring variables less confusing for beginners because it sounds more like natural language. Also note that you no longer put a semi-colon to end a statement.

Declaring an int variable

Objective-C

In Objective-C, you always have to explicitly specify the type of variable you’re declaring.

Swift

In Swift, the “var” keyword is used to declare a variable and you don’t have to specify the type if it can be inferred from what you’re assigning it. Also notice in Swift, we’re using the String class as opposed to NSString.

Assigning to a variable

Notice that when creating a string literal to assign to the string variable, we don’t need the “@” for Swift.

Objective-C

Swift

Declaring and assigning constants

In Swift, use the let keyword to declare constants.

Objective-C

Swift

Classes

Defining a class

A notable difference in class definition with Swift is the absence of the header (.h) and implementation (.m) file.

Objective-C

A header (.h) file

An implementation (.m) file

Swift

Creating a new class instance

Creating new classes in Swift is done with the classname followed by a pair of parentheses.

Objective-C

Swift

Subclassing

Subclassing is very similar in both languages.

Objective-C

In the header (.h) file

Swift

Methods

Declaring a method

Declaring a method is easier for beginners to understand as well. Notice in the Swift example how the return type of the method is specified.

Objective-C

In the header (.h) file

In the implementation (.m) file

Swift

Calling a method

Often, beginners are confused as to when to use square brackets. Swift does away with the square brackets for calling methods and instead uses the dot-notation we’re accustomed to seeing with properties. Notice the parentheses in the Swift example for calling a method.

Objective-C

Swift

Declaring a method with multiple parameters

A Swift method with multiple parameters is similar to Objective-C in the way that it’s structured in pairs of label:parametername

Objective-C

Swift

Calling a method with multiple parameters

Objective-C

Swift

Overriding a method

In Objective-C, you can override a method of the super class by just defining a method with the same method signature in the subclass. In Swift however, we have to explicitly put the override keyword so that it’s clear that it’s an overridden method.

Objective-C

In the implementation (.m) file

Swift

Properties

Declaring a property

Declaring properties with Swift is pretty straight forward. In fact, it looks just like declaring a variable except where you declare it is what makes is a property. In Swift, every property needs to be assigned a value either during declaration or in the initializer.

Objective-C

In the implementation (.m) file

Swift

Overriding the getter and setter

In modern Objective-C, the getter and setters are automatically synthesized for you but you can override them. The same is true for Swift and the syntax is a little easier on the eyes because it’s grouped together with the declaration.

Objective-C

Swift

What’s Next

In the next part of the series, we’ll look into the Swift equivalents of some Objective-C control structures and collections.

46 Shares
Share25
Tweet
+15
Share7
Pin
Buffer9