Introduction to Swift for developers: an Apple’s revolution

Introduction to Swift for developers: an Apple’s revolution

Apple released Swift in WWDC 2014. A new, fresh, and safe language that aims to be productive and maintainable. A language that will actually make you want to code.

swift apple

In 2014 there were a lot of projects, startups, and big companies that needed to be in every smartphone in the world. By that time, we were already able to do everything from ordering a cab to booking a hotel just by opening an app. We went to the movies, exchanged money, and talked to our family on the other side of the world from the comfort of our couch.

That was amazing for the final user and still is these days, but for the people developing those apps, it was not so wonderful. At least, every app needs to run on iOS and Android, which meant people writing in Objective-C and Java. The fact that an app for the futuristic today we are talking about needs those old languages with all their problems is really annoying, taking into account that we already have wonderful tools like JavaScript or Angular.js.

There comes Apple, releasing Swift in WWDC 2014. A new, fresh, and safe language. A language that aims to be productive and maintainable. A language that will actually make you want to code.

But why? Why would I move to Swift?

Swift has a fluid syntax that makes it easy to read. Without a doubt, it was inspired in Python and Ruby. Objective-C was built on C, so it has the heritage of an old language intended for low-level programming. Swift was not built on C, so it can drop all unnecessary keywords and syntax elements that are difficult to read.

Apart from that, Swift pointers are better handled. In Objective-C, if you try to call a method of a nil (also known as null in other languages) pointer, it just does not do anything. This was intended to prevent the application from crashing. However, this also caused numerous bugs since it leads to unpredictable behavior. Additionally, the developer must take care of documenting the code if a certain return value can be nil, using comments and naming conventions for methods.

To solve these problems, Swift introduces Optional types, which you may know if you have worked with Java 8 or .NET. This type explicitly allows nil to be a value. Since it can be used in the signature of a method as the return type, the developer that calls this method will know upfront that he may expect a nil value. Besides, Swift raises a runtime exception if the application is trying to reference a nil optional value, reducing the time consumed by bugs of this nature.

Apart from that, Swift is open source since version 3. This means that people all over the world can now contribute to make it a better language. It will help us to have a better and safer language, driven by the needs of the community.

So, let us see some code

As we said before, Swift has a friendly syntax. Here are the basics for this language.

print("Hello World!")

This single line is enough to build an app. No “import from standard libraries” are needed to make a simple console printing. Semicolons are optional. Literal strings must be written between double quotes.

let greeting = "Hello "

var name = "Alberto"
print(greeting + name + "!")
//Prints: Hello Alberto!

name = "Daniel"
print(greeting + name + "!")
//Prints: Hello Daniel!

var is the keyword to declare a variable, while let declares a constant. If there is no var neither let, the compiler will behave as if they existed. However, it is strongly recommended to use them for understanding purposes. If there is no var, someone reading our code may confuse a variable declaration with a simple assignment.

It is not needed to write the type of the variable, since the compiler can guess it from the value assigned. But we can make the type explicit. Also, you may declare multiple variables separated by commas.

var age: Int = 24, name: String = "Alberto"

Arrays and Dictionaries

Arrays are declared using []. They are lists of a certain type.

var languages = ["Swift", "Python", "Ruby"]
//Prints: Swift

Dictionaries, on the other hand, are collections of key-value pairs. Like JSON objects.

var employee = [
    "first_name": "Alberto",
    "last_name": "Cruz",


//Prints: Optional("Alberto")

As you see, when trying to access a member of the dictionary, we get an optional type. We are going to see how to deal with optional types in another section.


When writing if statements, we need no parenthesis.

var number = -4

if number > 0 {
} else if number < 0 {
} else {

//Prints: Negative


It is not recommended to use switch unless it is really necessary. However, Swift provides us with this structure.

var language = "Swift", createdBy = ""

switch language {
    case "C#":
        createdBy = "Microsoft"

    case "Java":
        createdBy = "Sun Microsystems"

    case "Swift":
        createdBy = "Apple"

        createdBy = "someone else"

print(language + " was created by " + createdBy + ".")
//Prints: Swift was created by Apple.


This is a for-cycle with a fixed amount of loops.

for i in 0...9 {

//Prints all numbers from 0 to 9. Each of them in a new line.

In this example, we are creating an array of numbers using 0…9. So we can also iterate any array using a for structure.

var languages = ["Swift", "Java", "C#"];

for language in languages {

//Prints: Swift

While-loops are also provided by Swift.

var number = 4

while number > 0 {
    if number % 2 == 0 {
        print(String(number) + " is an even number.")

    number -= 1

//Prints: 4 is an even number.
// 2 is an even number.

Just for the record, we are converting an integer value into a string by using String(number).


Functions are declared using the func keyword. After the parameters, we put a -> operator to indicate the return type.

func sum(firstNumber:Int, secondNumber:Int) -> Int {
    return firstNumber + secondNumber

print(sum(firstNumber:1, secondNumber:2))
//Prints: 3

Functions may receive an undefined number of parameters of the same type.

func printNames(names: String ...){
    for name in names {
        print("Printing: " + name)

printNames(names:"James", "Kirk", "Robert", "Lars")
//Prints: Hi James!
// Hi Kirk!
// Hi Robert!
// Hi Lars!


Swift also supports object-oriented programming by using classes.

class Dog {
var name: String
var age: Int
init(name:String, age:Int) { = name
self.age = age

func bark() -> String {
return "Woof!"

var spot = Dog(name:"Spot", age:5)
//Prints: Woof!

Properties are declared as if they were variables and methods as functions. init is the keyword used for constructors. self is a pointer that refers to the instance of the object. Just like this in most languages.

Inheritance is easy. Looks as if we were declaring a class of a certain type.

class Beagle: Dog {
    override func bark() -> String{
        return "Woof, Woof!"

var buddy = Beagle(name:"Buddy", age:7)
//Prints: Woof, Woof!

Optional Types

Swift introduces optional types. A variable of an optional type may contain a certain value, or no value at all. No value is represented by nil in Swift, just like null in other languages.

class Person {
    var name:String = "Nancy Callaghan";

var nancy: Person?

That is how we declare an optional variable. Below is how we can check whether the variable nancy has a value or not, before access to its property.

if let name = nancy?.name{
} else {
    print("No person to call.")

//Prints: No person to call.

If we happen to be sure that an optional variable is not nil, then we can force the unwrapping of the value by using ! operator. If there is no value, a runtime exception will be thrown.

var nancy: Person? = Person()
//Prints: Nancy Callaghan

Final Words

Swift is a big step in programming history. We are experiencing a programming language created by Apple, which is an open source. Ten years ago, we could not imagine Apple having anything open source. Besides, it is a great effort in favor of the community. Swift being open source means that some day, we can run a server-side application running on Windows, as we can do now with Node.js.

What is more, there is a framework called Vapor that helps us make web applications. We will show how to do so by using Swift in our next post.


Comments?  Contact us for more information. We’ll quickly get back to you with the information you need.