How to return an outer function inside an asynchronous inner function in swift?

前端 未结 2 1468
野趣味
野趣味 2020-12-21 05:50

I am creating a swift class which contain of a function which validate if the user is true. However, the userVerifyResult will always return \"false\" even if t

相关标签:
2条回答
  • 2020-12-21 06:08

    Use completion handler, as Apple does in their frameworks:

    func verifyUser(completionHandler: ((result: Bool)->())) {
    

    then return the result via

            if(json["valid"]! == "true"){
                completionHandler(result: true)
            }else{
                completionHandler(result: false)
            }
    

    then invoke it as

    verifyUser { (result) -> () in
        println(result)
    }
    
    0 讨论(0)
  • 2020-12-21 06:24

    There is no way to block a NSURLSessionDataTask (that i know of) until you return the function, so its not possible to do it like that. You will have to have a callback in your tasks completion handler that will print the result or delegate the printing to another function

    0 讨论(0)
提交回复
热议问题