O'Reilly logo

Application Development with Swift by Hossam Ghareeb

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The code structure

Before starting to write Swift, you have to be aware of its structure, as it is a very important thing to know in any programming language.

As we said earlier, Swift is not a superset of C. For sure it's influenced by C and Objective-C but easier and fun to use than both.

I will share with you a piece of code in Swift to show its structure:

import UIKit

let pi = 3.14
//Display all even numbers from 0 to 10
for i in 0...10
{
    if i % 2 == 0
    {
        println("\(i) is even")
    }
}

func sayHello(name: String = "there")
{
    
    println("Hello \(name)")
}

sayHello(name: "John") // will print "Hello John" 
sayHello() //Will print "Hello there", based on default value

Tip

Downloading the example code

You can download the example code files from your account ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required