-2

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?

rob mayoff
  • 342,380
  • 53
  • 730
  • 766
S Kumar
  • 1
  • 1
  • 4

3 Answers3

3

For those of you viewing this post three years later as I did, the Swift functions have been improved. Now you can write:

DispatchQueue.main.async(execute: { () -> Void in
    // Your code here
})
Mark Radbourne
  • 477
  • 2
  • 7
2

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")
}
rob mayoff
  • 342,380
  • 53
  • 730
  • 766
1

Xcode gives you an easy help with that....

Just type in

dispatch_async and hit enter... Than XCode gives you something like this...

dispatch_async(queue: dispatch_queue_t!, block: dispatch_block_t! () -> Void)

now take your mouse and double-click on block: dispatch_block_t! () -> Void and XCode will automatically change this to a working closure expression :-)

dispatch_async(dispatch_get_main_queue(), { () -> Void in
     //Your Code here       
})
Dennis Weidmann
  • 1,932
  • 1
  • 12
  • 16