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

前端 未结 2 1473
野趣味
野趣味 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)
    }
    

提交回复
热议问题