Learn Swift from Objective-C : Conditionals, Control Structures and Collections

learn_swift_conditionals_controlflow_collections

This tutorial is a continuation from Part 1 where we went through a comparison between Swift and Objective C for various operations involving variables, classes, methods and properties.

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.

Control Flow

Writing an IF statement

Objective-C

Notice in the second example that the If statement tests if the variable “aString” is empty.

Swift

In Swift, we need to denote that a variable has an optional value with a question mark. This means that it could be empty.
Then we use the let keyword and a name to test if aString is empty.

Writing a Switch statement

In Swift, switch statements are a lot more flexible and can be used to test a variety of comparison operations. In Objective-C, switch statements were limited to integer cases.

Objective-C

Swift

Notice that you don’t need the break statements anymore!

Loops

Writing a For loop

Objective-C

Swift

Notice the second swift example below. It’s equivalent to the first example. The .. notation can signify a range.

Writing a while or do while loop

The while loop syntax in Swift is almost identical to Objective-C (the Swift syntax drops the parentheses).

Objective-C

Swift

Collections

Declaring an array

Arrays in Objective-C used the NSArray and NSMutableArray classes and each array could contain a mixture of objects of different class types. In Swift, there’s one Array class and you have to specify the type of objects that the array will contain.

Objective-C

Swift

Notice that in the second and third examples, we don’t specify that the variable is of type String Array uses because it can be inferred from the array that we’re assigning it. From the assigned array items, Swift can infer that the variable is an Array and it contains String objects.

Even in example 1, we don’t need to specify the variable type because it can be inferred from what we’re assigning to it.

Adding and inserting into an array

Objective-C

Swift

Notice in the second example that you can use += to append objects into the array.

Declaring a dictionary

Just like Arrays in Swift, Dictionaries need to be type safe so you have to define what class types the dictionary is going to use for its keys and values.

Objective-C

Swift

Adding and Assigning to a dictionary

Assigning to a dictionary in Swift is very similar to Objective-C.

Objective-C

Swift

Notice in Swift, you can use the same syntax to add a key/value pair or reassign an object for a particular key.

What’s Next

In the next part of the series, we’ll look at protocols and delegation in swift!