简体   繁体   English

Swift:在GCD完成块中返回布尔值

[英]Swift: Return boolean in GCD Completion Block

I have a function written in Swift. 我有一个用Swift编写的函数。 I want the completion block to return a boolean. 我希望完成块返回一个布尔值。 How can I go about doing this? 我该怎么做呢? I am using Grand Central Dispatch. 我正在使用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
    // ...
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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