Learn Swift by Examples: #2


Arrays

import UIKit

let array = [10, 20, 30]

var stringArray = ["Apple", "Google", "Microsoft"]

var intArray : [Int]
intArray = [99, 999, 9999]
intArray [0] = 3

// appending element at the end
intArray.append(9999)
intArray += [99999]

// insert in a position
intArray.insert(9, atIndex: 0)

// remove items 
intArray.removeLast()
intArray.removeAtIndex(0)

println("Array size: \(intArray.count)")

if intArray.isEmpty {
    
}

for item in intArray {
    println(item)
}

Map/Dictionary

import UIKit

var aMap = ["key1" : "value1", "key2" : "value2"]


var anotherMap : [String : String]
var keyInt : [Int : String]

println (aMap ["key1"])

aMap ["key3"] = "value3"
aMap.updateValue("value4", forKey: "key4")
aMap.updateValue("value5", forKey: "key4")

// removing an element
aMap ["key2"] = nil
// removed the value and key as well 
// another way to do the same thing
aMap.removeValueForKey("key1")

for (key, val) in aMap {
    println("Key: \(key) and value: \(val)")
}

Tuple

import UIKit

var aTuple = ("Hello", "world", 12)

println(aTuple.0)
println(aTuple.1)
println(aTuple.2)

func getATuple() -> (String, Int, String) {
    return ("Hello", 22, "world")
}

let result = getATuple()

println("param#1: \(result.0) param#2: \(result.1) param#3: \(result.2)")


func getAnotherTuple() -> (name: String, age: Int) {
    return ("Hello", 22)
}

let res = getAnotherTuple()

println("param#1: \(res.name) param#2: \(res.age)")

let (name, age) = getAnotherTuple()

println("param#1: \(name) param#2: \(age)")

Optionals

var dontKnowIntVar : Int?

dontKnowIntVar = 5

if dontKnowIntVar != nil {
    println("value is: \(dontKnowIntVar)")
    println("value is: \(dontKnowIntVar!)")
}

Enum

enum MaritalStatus {
    case Single
    case Married
    case Widowed
    case Divorced
    case NotGiven
}

var status : MaritalStatus

status = MaritalStatus.Married
status = .Divorced

switch status {
case .Single:
    println("Single")
case .Married:
    println("Married")
case .Widowed:
    println("Widowed")
case .Divorced:
    println("Divorced")
case .NotGiven:
    println("NotGiven")
    
}

Closure

let simpleClosure = {
    println ("Hello world!")
}

func simpleFunc ( aClosure : ()->()) {
    for i in 1...5 {
        aClosure()
    }
}

simpleFunc(simpleClosure)

simpleFunc({
    println ("Hello world!")
})

let anotherClosure = { () -> () in
    println("Another closure")
}

let unsortedArray = [5, 2, 1, 7, 6, 3]

let sortedArray = sorted(unsortedArray, {
    (first: Int, second: Int) -> Bool in
    return first < second
})

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