Episode #189

Swift 2 Functions and Extensions

15 minutes
Published on October 1, 2015
In this free episode, Ben explores Swift 2 functions, how argument labels work, returning tuples, the guard clause, as well as extending behavior on core types.

Episode Links

func greetWithName(name: String, _ suffix: String) -> String {
    return "Hello, \(name)\(suffix)"
}

let greeting = greetWithName("Ben", "!")


func minMax(array: [Int]) -> (min: Int, max: Int)? {
    guard !array.isEmpty else {
        return nil
    }

    var min = array[0]
    var max = array[0]

    for item in array[1..<array.count] {
        if item > max {
            max = item
        }

        if item < min {
            min = item
        }
    }

    return (min, max)
}

if let (min2, max2) = minMax([1,10,-2,44,3]) {
    min2
    max2
}


func printMessage(msg: String, times: Int) {
    for _ in 0..<times {
        print(msg)
    }
}

printMessage("Ho", times: 3)



func doSomething(n: Int) {
    print("Ho")
}

extension Int {
    func times(block: (Int) -&gt; ()) {
        for n in 0..<self {
            block(n)
        }
    }
}

15.times {
   print("Hi, \($0)")
}

Want more? Subscribers can view all 585 episodes. New episodes are released regularly.

Subscribe to get access →

Source Code

View on GitHub Download Source