vinhnx / swift_learning_notes

Notes from learning Swift
https://github.com/vinhnx/swift_learning_notes/issues
0 stars 0 forks source link

Labeled loops and @noscape #5

Open vinhnx opened 8 years ago

vinhnx commented 8 years ago

https://realm.io/news/tryswift-hector-matos-hipster-swift/

func noEscape(doSomeThing: @noescape() -> ()) {
    // domeSomething closure MUST be called in the body of this function
    // or in a function immediately called from here
    doSomeThing()
}

noEscape { 
    print("hello")
}

func labelsLoop(foo: Array<Int>, bar: Array<Int>) -> Int {
    var result = 0

    fooLoop: for fu in foo {
        barLoop: for ba in bar {
            if ba > fu {
                result = ba
                // works for continue also
                break fooLoop
            }
        }
    }

    return result
}

labelsLoop(foo: [1, 2, 9], bar: [4, 5, 6]) // result: 4