Swift is a modern programming language developed by Apple to create the next generation of iOS and OSX applications. Swift is a fairly new language and still in development but it clearly reflects the intentions and the future direction. This article will revolve around the basic concepts in the Swift language and how you can get started.


Swift allows you to declare variables and constants. Constants utilize the "let" keyword and variables uses the "var" keyword as shown below:

Swift has built in type inference so you don't have to write the type with the declaration. The above code can be written as follows:

Since, greeting is defined as a constant, you will see a compile time error if you try to change it. Take a look at the following example where we have created an instance of class called Customer using the "let" keyword and assigned it's properties.

Do you think the above code will result in a compile time error since we are changing the properties associated with Customer instance?

The answer is "NO" the above code will not throw any exception since the customer object is still pointing to the same object. On the other hand the following code will throw a compile time error:


In Swift methods are defined using the func keyword. The following implementation defines a method which has no parameters and does not return anything.

Let's change the above method so it takes in and return an argument.

Build the app and you will notice that it gives you compile time errors indicating that you cannot change the value of a let arguments. By default all the parameters are passed in as let parameters. As we have learned in the previous section that you cannot change the value of let instances once they are declared. In order to fix this issue you will indicate to the func that the message parameter is a "var" parameter as shown below:

Just like single parameters your function can take multiple parameters as shown in the implementation below:

Functions are also capable of returning multiple parameters as shown in the implementation below:

The functions that we have declared above are all instance methods. You can also declare static functions which are class methods. The static methods are declared using the "class" keyword as shown below:

Now, you can call the above "getById" method using the following syntax:


Closures in Swift language are a replacements for "Blocks" in Objective-C. Closures are self contained blocks of functionality that can be passed around and used in code. Let's take a look at a very simple example of Closures in Swift language. Assume that you have an array and you want to sort the items in the array in descending order. The array exposes a sort method which is defined as follows:

The Array sort method reflects that it can take a function which has two input parameters and a boolean return value. We can easily create such a function and feed it to the sort function of the Array.

Now, you can use the sortDescending function in your sort function as shown below:

All of the above works and you get the expected result but it is too much work. It would be great if we can do everything inline without having to create a separate function. This is where Closures steps in for the rescue. Take a look at the following code which provides the same solution using Closures.

You might find Closures syntax a little difficult to understand at first but just remember that Closure is defined within "{}" and the body starts after the "in" keyword. Since, Swift has the ability for type inference the above code can be simplified as follows:

We have removed the "String" and "Bool" type keywords since Swift can infer those types. You can even remove the "return" keyword since Closures return the last statement in their body by default.

The above still feels very heavy on the parameters. You can further simplify by using the special Closure parameters syntax as shown below:

The above Closure simply represents that it will take two parameters and the action between the two parameters will be using the ">" operator. You can even simplify further using the following syntax:

Although the above code provides the same result we believe that the code does not convey the purpose clearly. We recommend you use the syntax that you are comfortable with which reflects the purpose clearly.


Swift brought many modern language features to iOS development. Swift language is still under development and is getting better with every release. We highly recommend learning Objective-C language but with Swift Apple has shown their future intentions and direction. In the future we will cover different aspects of Swift programming language in detail.