Search code examples
swiftgrand-central-dispatchdispatch-async

How to declare `dispatch_function_t` for `dispatch_async_f` in Swift?


Xcode 7 beta 5. I'm attempting to use dispatch_async_f to avoid a block.

func myFirstFunc() {
    let identifier = QOS_CLASS_BACKGROUND
    let queue = dispatch_get_global_queue(identifier, 0)
    let context: UnsafeMutablePointer<Void> = nil
    let work: dispatch_function_t = myOtherFunc
    dispatch_async_f(queue, context, work)
}
func myOtherFunc(context: UnsafeMutablePointer<Void>) {
}

Got an error:

A C function pointer can only be formed from a reference to a 'func' or a literal closure


Solution

  • It looks like myOtherFunc is a method of a class, is that right? This will only work if myOtherFunc is declared as a free function at the top level.