Learn Swift by Examples: #1


import UIKit

/*
Variable starts with a "var" keyword
*/
var anInt = 5

/*
"anInt" variable can not be changed to some other data type other than Int
*/

/*
Swift is a type safe (strongly typed) language
*/

/*
Swift infers the type from the initial value
*/
var aString = "Hello World" // inferred as String variable
var aBool = false // inferred as Bool variable

/*
If there is no initial value then we have to explicitly specify the data type in variable declaration
*/
var anIntegerVariable : Int
var aStringVariable : String
var aFloatVariable : Float
var aDoubleVariable : Double
var aCharVariable: Character
var aBooleanVariable: Bool

/*
declaration and initialization together
*/
var anotherVariable : Int = 5
var anotherString : String = "Bye world"
var numberOfItems : Int = 20

/*
lets use a constant. Constant starts with keyword "let"
*/
let constantVariable = 5
let anotherConstant : String = "Can not change value"

/*
Using constant is recommended. Use constant whereever possible. Although we use term variable, but in many cases we don't change the value of a variable in its lifetime. Do use constant in such cases. Behind the scene, there are some performance optimization options for the compiler is you use constants.
*/

// print "Hello world!"
println("Hello world!") // Hello world!

println(anInt) // 5

// formatting print output 
var myName = "King"
var myAge = 7

// put the variable inside parenthesis with a preceeding back-slash
println("I am \(myName) and my age \(myAge)")
// I am King and my age 7

// we can perform operations inside formatting
println("After 5 years I will be \(myAge + 5)")
// After 5 years I will be 12

var intVar = 5
var doubleVar = 2.5

// var result = intVar * floatVar
// ERROR: Can not cast implicitely

var result = intVar * Int(doubleVar)
// or, 
var result2 = Double(intVar) * doubleVar


/*
if condition
*/
var persons = 10

if persons < 5 {
    println("persons are less than 5")
} else {
    println("persons are greater or equals to 5")
}

// Do not need parenthesis around the conditions but curly braces are required. Even if single statement!

var a = 0

/*
if a {
    
}
// ERROR: 0 is an Integer, not a boolean
*/

// correct way
if a == 0 {
    
}

// multiple conditions 
if a == 0 && persons < 5 {
    
}

// Switch conditions 
let constantVar = 10

switch constantVar {
case 0:
    break;  // break is mandatory
case 1:
    break;  // break is mandatory
case 2:
    break;  // break is mandatory
    
default:    // default is mandatory, unless you have cases all possible cases
    break
}

// Switch with ranges
let anotherConst = 10

switch anotherConst {
case 0...3: // inclusive: 0, 1, 2, 3
    break
    
case 4...5:
    break
    
default:
    break
}

// Loops
for var i = 0; i < 5; i++ {
    
}

for i in 1...100 { // notice the three dots
    // 1 to 100 inclusive
}

for i in 0..<100 {  // notice the two dots
    // last one exclusive, 0 to 99
}

var name = "King"
for eachChar in name {
    println(eachChar)
}

// Functions 
// no params 
func aFunction () {
    println ("a Function")
}

// one param 
func aFunction (name : String) {
    println ("name: \(name)")
}

// By default the function parameters are constant!!!

func aFunction (x : Int) {
    //x = 5 // ERROR
}

// to make the params a variable type var in front of it 

func anotherFunction (var x : Int) {
    
}

// return from function 
func add(a : Int, b : Int) -> Int {
    return a + b
}

// default value 

func addAll(a : Int = 5, b : Int = 10, c : Int = 2) -> Int {
    return a + b + c
}

addAll() // OK, return 5 + 10 + 2 = 17
// addAll(5) // NOT OK
addAll(a : 1, b : 2, c : 3) // OK, return 6
addAll(a: 1) // OK, return 1 + 10 + 2 = 13 
addAll(a: 1, c : 1) // OK, return 1 + 10 + 1 = 12

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s