Tutorials‎ > ‎

Swift, A Beginner’s Guide

posted Nov 1, 2015, 11:50 PM by Justinus Hermawan Sadeli   [ updated Aug 15, 2016, 11:38 PM by Surya Wang ]

Swift Logo

Swift is a new programming language for iOS, OS X, watchOS, and tvOS apps. Swift was introduced at Apple’s Worldwide Developer Conference (WWDC) 2014 as a replacement for Objective C, Apple’s long-time programming language. At WWDC 2015, Swift 2.0 was announced with a number of improvements and changes, including Apple’s commitment to make Swift an open source language.

In this tutorial, you will learn about the beginning of Swift programming, especially about how to make a Swift command-line app as the beginning to make a full iOS app with a beautiful user interface. This will provide you with the basic knowledge you’ll need to make the amazing apps you’ve dreamed of!

Getting Started

Note: The Swift version that I used in this tutorial is Swift 2.1 and the Xcode version is Xcode 7.

The very first step is to download Xcode (the software program that you write your apps in) for free on the Mac App Store.
SS 1

Be sure you have the latest version of Xcode. You need Xcode 6 or later to work with Swift. Once you have Xcode installed, open it and click the Get started with a playground option.

SS 2

Note: If you don’t see the Welcome to Xcode window, click Window -> Welcome to Xcode to display it.

A playground is a simple way to experiment and play around with Swift code. It allows you to edit the code listings and see the result immediately.

You can’t run a playground as an iPhone app, but it will be very important in helping you to understand the basic of Swift. Don’t worry, you’ll be creating your very own iPhone app soon enough!

Set the name to MyFirstPlayground, the platform to iOS, and click Next. Save the playground wherever you wish.

SS 3
Introduction to Playgrounds

You will see that the playground you created already has three lines of code:
 // Playground - noun: a place where people can play
 
 import UIKit
 
 var str = "Hello, playground"

Here’s the breakdown of the playground, one line at a time.

 // Playground - noun: a place where people can play

The first line, which starts with two forward slashes, is called a comment.

This line is only there for you, or any other programmers that look at your code, to see. It does not affect the way that your code functions. Think of it as “a way to write notes in your code”.

 import UIKit

The second line imports UIKit, which you can think of as “a bunch of code written by the smart folks at Apple.” All you need to know about this right now is that you need it for the rest of your code to work.

 var str = "Hello, playground"

The third line is the one you should focus on right now.

This line creates a variable named str that holds the value Hello, playground. On the right side of the window, you can see that Xcode is keeping track of what this variable holds.

SS 4

Try changing “Hello, playground” to “Hello, Swift!”, like this:

 var str = "Hello, Swift!"
SS 5

Did you see how the right side of the window changed what it said too? Congratulations, you just did some Swift programming!

Playground as a Calculator

Now let’s see what else you can do in playground. At the bottom of your file, try typing a basic math expression such as 9 % 5 (9 modulo by 5) and hit enter to go to the next line:

 9 % 5

The playground will give you the answer to your equation over in the shaded right side of the window:

SS 6

Cool, yeah? You can do any other extreme math operation too.

Challenge: Quick – try to use Playgrounds to show me the result of 1234 * 5678!

Using playground as a simple calculator is great and all, but now let’s move on and get into some more coding! Delete everything in the program except for import UIKit so you have a clean state to work with.

 import UIKit

It is extremely important that you leave that line there because the Playground will not work without it.

Variables

Next, it’s time for you to play with variables. You use variables to store values. When you create a variable, you always use the following syntax:

 var variableName: variableType = initialValue

Except you substitute the following:

·  variableName: Whatever you want to name the variable, like str or age for example.

·  variableType: The type of the variable, like String or Int. More on that in a moment.

·  initialValue: Whatever you want to set the initial value of the variable to, like “Hello, Swift!” or 22.

Try an example! Add these lines to the bottom of your playground:

 var str: String = "Hello, Swift!"
 var age: Int = 22

In the first line, you created a variable named str of type String, and set the initial value to “Hello, Swift!”

In the second line, you created a variable named age of type Int, and set the initial value to 22.

You’re starting to get a good idea how to make a variable, but you may be wondering what the difference between String and Int types is, and what other data types you can use.

Data Types

Here’s a list of some of the basic data types that Swift has to offer:

·  Int – whole numbers, or integers

·  Double – decimal numbers

·  Bool – a value that can be true, or false

·  String – a “string” of letters or words

Practice using these data types. Add the following lines to the bottom of your playground:

 var currentTime: Int = 12
 var costOfBurger: Double = 20.55
 var isHungry: Bool = true
 var name: String = "Kosur"

Notice how each of the values show up on the shaded right area of the window as the playground keeps track of them. Now, try changing the value of the String name.

Add the following line to the bottom of your playground:

 name = "Komala"

This line did not require the use of var or : String because the object was already created. All you did here was change the value that it was storing.

Challenge: Your turn to give it a try. At the bottom of the file, try creating a variable to represent your favorite movie.