Why Swift?

In case you haven’t heard, Apple just introduced a new language for iOS and OSX developers called Swift. It has come to light that Swift was in the works since 2010, which is 2 years after the first SDK was released. Apple saw the limitations of Objective-C which is almost 30 years old and decided it was time for a change. However, in true Apple fashion, they did not want to release a half-baked language. They realized that no matter the drawbacks of Objective-C, they could still push the envelope with it, and so they did.

It’s been six years since the release of the first SDK and 1.2 million apps have been submitted to the App Store. Millions of developers have suffered through learning the arcane syntax and limitations of Objective-C. Recently, a vocal few decided to speakup expressing their woes about the outdated language.

Swift probably took a little over four years to create and is the result of the work of many smart individuals who love crafting a new language. They looked all around for inspiration and not only created a new language but tools to along with it that would make it easy to learn.

When talking about Swift, Apple refers to three key considerations: Safe, Modern and Powerful. It lives up to all those three things. Outlined below are some of the very basics you need to get up and running with Swift. If you already know a programming language, then you will see a lot of similarities with other modern languages. You might even wonder why they had to invent a whole new language, but that is discussion for another blog post.

Using Swift

Firstly, you will have to download and install Xcode 6. Once you have installed it, open it up and select File from the menu -> New -> Select Source on the left under either iOS or OSX -> Playground. Give your playground a name and you are ready to get started.

Alternatively, you could use the REPL (Read Evaluate Print Loop) from the terminal.

Instructions to run from the terminal:

1. Open up terminal

2. If you have two or more versions of Xcode installed then you will need to select Xcode 6 as your default version. If you are only running Xcode 6 then skip ahead to step 3. If you are only running Xcode 6 then skip ahead to step 3, otherwise go ahead and run the following line:

At the time of writing this post beta version of Xcode 6 was named “Xcode6-Beta”. Please check your app name in the “Applications” folder to write out the appropriate path when using xcode-select.

3. To start the REPL type:

xcrun swift

Fundamentals

Variables

As with every programming language you have variables which allow you to store data. To declare a variable you have to use the keyword var.

var greeting: String = "Hello World"

The above code instructs the system that you want to create a variable named greeting which is of type String and it will contain the text, “Hello World”.

Swift is smart enough to infer that if you are assigning a string to a variable and in fact that variable will be of type string. So you need not explicitly specify the type as in the above example. A better and common way of writing the above example would be:

var greeting = "Hello World" // Inferred type String

Variables can be modified once created so we could add another line and change our greeting to something else.

While writing an application there are many instances where you don’t want to change a variable once it has been initialized. Apple has always had two variants of types mutable and immutable. Mutable meaning the variable can be modified and immutable that it cannot be modified. They prefer immutability by default which means that the values aren’t going to change and it makes your app faster and safer in a multi-threaded environment. To create an immutable variable you need to use the keyword let.

If we change our greeting example to use let instead of var then the second line will give us a compiler error because we cannot modify greeting.

The above example not only shows us the various types that are available in Swift but it also shows us that the reason to use let. Aside from the version number of the Swift language everything else remains constants. You might argue that isAwesome is debatable but I’ll let you reach that conclusion once you reach the end of this post.

In all the above examples, I have been using the keyword let which means you cannot modify the string once it has been created. However, if you do need to modify the string then simply use the keyword var.

Other Types

Besides strings you have Int for whole numbers. Double and Float for floating-point numbers and Bool for boolean values such as: true of false. These types are inferred just as a string so you need not explicitly specify them when creating a variable.

A Float and Double vary in precision and how large of a number you can store.

Float: represents a 32-bit floating-point number and the precision of Float can be as little as 6 decimal digits.

Double: represents a 64-bit floating point number and has a precision of at least 15 decimal digits.

By default when you write a floating-point number it is inferred as a Double.

var version = 1.0 // inferred as Double

You can explicitly specify a Float.

var version: Float = 1.0

Collection Types

Array

Collections come in two varieties. Firstly, an array which is a collection of data items which can be accessed via an index beginning with 0.

You can create two types of arrays: an array of a single type or an array with multiple types. Swift is keen on being safe so it prefers the former but can accommodate the later with generic types. The example above is an array of strings which means that it is a single type array.

To access an item from the array you need to use the subscript:

println(cardNames[0])

Note: we used a function above called println which will print the value “Jack” to the console and then add a new line.

Modifying an Array

Lets create a new array that contains a todo list.

var todo = ["Write Blog","Return Call"]

Make sure that you use the keyword var so that we can modify our array.

To add another item to our todo array we use the ‘+=’ operator:

todo += "Get Grocery"

To add multiple items to our todo array we simply append an array:

todo += ["Send email", "Pickup Laundry"]

To replace an existing item in the array simply subscript that item and provide a new value:

todo[0] = "Proofread Blog Post"

To replace an entire range of items:

todo[2..<5] = ["Pickup Laundry","Get Grocery", "Cook Dinner"]

Dictionary

The other collection type is a Dictionary which is similar to a Hash Table in other programming languages. A dictionary allows you to store key-value pairs and access the value by providing the key.

For example, we can specify our cards by providing their keys and subsequent values.

var cards = ["Jack" : 11, "Queen" : 12, "King" : 13]

Above we have specified the card names as the keys and their corresponding numerical value. Keys are not restricted to the String type, they can be of any type and so can the values.

Modifying a Dictionary

What if we wanted to add an “ace” to our cards dictionary? All we have to do is use the key as a subscript and assign it a value. Note: cards is defined as a var which means it can be modified.

cards["ace"] = 15

We made a mistake and want to change the value of “ace”. Once again just use the key as the subscript and assign it a new value.

cards["ace"] = 1

To retrieve a value from the dictionary

println(cards["ace"])

Control Flow

Looping

What it good is a collection if you cannot loop over it? Swift provides while, do-while,for and for-in loops. Lets take a look at each one of them.

The easiest one of them is the while loop which states while something is true execute a block of code. It stops execution when that condition turns to false.

while !complete {
println("Downloading...")
}

Note: the exclamation mark before the variable complete denotes not and is read as “not complete”.

Likewise, you have the do-while loop which ensures that your block of code is executed at least once.

If Statements

Note: The if syntax can have parenthesis but they are optional. However, the braces {} are mandatory unlike other languages.

Switch Statement

The switch statement in Swift is very versatile and has a lot of features. Some basic rules about the switch statement:

It doesn’t require a break statement after each case statement

The switch is not limited to integer values. You can match against any values such as: String, Int, Double or any object for that matter.

The switch statement must match against every value possible if not you must have a default case which makes your code safer. If you don’t provide a case for every value or a default then you will get a compiler error saying: “switch must be exhaustive”.

What if we simply wanted our function to build a string and return the value instead of printing it? Then we can specify a return it which is specified at the end of the function declaration followed by an array ->.

Above we are saying that we are creating a function named buildCard which takes in two parameters and returns a String.

Conclusion

If you have made it this far, then congratulations! You are now well-versed in the basics of Swift. That is a lot to take in, but it is only scratching the surface and what Swift is capable of. There is so much more to learn, and here at Treehouse we are working on courses to provide you with all the Swift learning that you need. Stay tuned for more!

If you want to learn the basics of Swift, take Amit’s Learn Swift courses on Treehouse.

The link for Xcode 6 doesn’t have any Xcode 6 download links? Has something changed since this post – I only see and Xcode5 download option….
This will be my first go at creating an app, any other helpful tips are appreciated.

This is a great primer for those who are coming to Swift. After going through your Objective-C course, it’s even more understandable coming from Objective-C to Swift. Perhaps this notion will change but I still think that if you’re a developer in iOS, it is beneficial to know, read and understand Objective-C as it will provide you with an incredibly strong foundation for moving forward with Swift.

The playground is very buggy and I prefer using the REPL. Note that the println command does not show anything in the preview pane. It only displays in the console which is also displayed in the assistant editor so not sure if you have that pulled up. To display the assistant editor you can either use the shortcut Command+Shift+Enter or use the menu View -> Assistant Editor -> Show Assistant Editor.

Each is designed by our faculty of tech professionals to guide even a coding beginner to becoming a job-ready software developer armed with a professional portfolio of real-world projects. Try one out today with our free seven-day trial, and see if software development is for you.