简体   繁体   中英

Swift: Return boolean in GCD Completion Block

I have a function written in Swift. I want the completion block to return a boolean. How can I go about doing this? I am using Grand Central Dispatch.

func myFunc() -> Bool 
{
    var success:Bool = false 

    // code here 

    dispatch_async(dispatch_get_main_queue(), { 
        return success
        )}
    )}
}

thanks!

Standard why of dealing with this async nature is not to return value, but pass in completion handler:

func myFunc(completion:(success: Bool) -> ()) {
    var success:Bool = false

    // code here

    dispatch_async(dispatch_get_main_queue()) {
        completion(success: success)
    }
}

Then work with it:

myFunc({ (success) in
    // ...
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM