Search code examples
iphoneswiftxcode6dispatch-async

dispatch_async in Swift


I am really new to this programming language called swift.

I use

dispatch_async(dispatch_get_main_queue(), ^{
});

this method for async-dispatch-queue in xcode 5.

I want implement it in swift language.

how do i implement dispatch queue in or swift?


Solution

  • You can use this syntax:

    dispatch_async(dispatch_get_main_queue(), {
        println("hello")
    })
    

    However, when the last argument is a block, Swift lets you put it outside the parentheses. This makes the function seem more like a control structure (like a for or if statement). Thus you can do this:

    dispatch_async(dispatch_get_main_queue()) {
        println("hello")
    }