在函数外部使用Alamofire请求的值

时间:2015-11-25 22:27:51

标签: ios swift asynchronous alamofire completionhandler

我似乎无法解决这个问题:我正在尝试使用Alamofire请求从我的服务器(使用JSON)中获取错误,但我无法获得该函数之外的值。

这是我的功能的实现:

    func alamoRequest(username : String, email: String, password: String, facebook: String, completionHandler : (String?) -> Void) {

    var jsonValue : JSON?
    let URL = "http://someurl.com/login.php"
    let requestParameters = ["username" : username, "email" : email, "password" : password, "facebook" : facebook, "date": NSNull()];
    var jsonString : String = String()

    Alamofire.request(.GET, URL, parameters: requestParameters).validate().responseJSON {
        response in
        switch response.result {
        case .Success:
            if let value = response.result.value {
                jsonValue = JSON(value)
                jsonString = jsonValue!["error"].stringValue
                print("Value in implementation is: \(jsonString)")

            }
        case .Failure(let error):
            print(error)
        }
    }
    completionHandler(jsonString)
}

以下是我的称呼方式:

logic.alamoRequest(usernameField.text!, email: emailField.text!, password: passwordField.text!, facebook: "false") {
            json in
            jsonString = json!
      }

        alamoRequest = jsonString
        print("Value in call is: \(alamoRequest))

但输出总是这样:

Value from call is: Value in implementation is: Email already exists

我怎样才能"返回"来自函数的东西还是等待它执行而不阻塞UI?

1 个答案:

答案 0 :(得分:5)

你的操作是 async ,等待完成的方式只是使用闭包,你试图在闭包之外返回值,所以还没有检索到值!并且这是你的空值的原因,你可以在你的闭包中“抛出”completionHandler,如下例所示:

func alamoRequest(username : String, email: String, password: String, facebook: String, completionHandler : (String?) -> Void) {

   var jsonValue : JSON?
   let URL = "http://someurl.com/login.php"
   let requestParameters = ["username" : username, "email" : email, "password" : password, "facebook" : facebook, "date": NSNull()];
   var jsonString : String = String()

   Alamofire.request(.GET, URL, parameters: requestParameters).validate().responseJSON {
    response in
       switch response.result {
       case .Success:
           if let value = response.result.value {
               jsonValue = JSON(value)
               jsonString = jsonValue!["error"].stringValue
               print("Value in implementation is: \(jsonString)")
               completionHandler(jsonString)
           }
       case .Failure(let error):
           print(error)
       }
   } 
}

尽管如此,以上述方式,您不知道请求中是否存在某些错误,因为您的completionHandler仅在.Success案例中被调用,如果您希望可以调用它总是在case的最后enum之后。

Alamofire.request(.GET, URL, parameters: requestParameters).validate().responseJSON {
    response in
       switch response.result {
       case .Success:
           if let value = response.result.value {
               jsonValue = JSON(value)
               jsonString = jsonValue!["error"].stringValue
               print("Value in implementation is: \(jsonString)")

           }
       case .Failure(let error):
           jsonString = nil
       }
       completionHandler(jsonString)
   } 
}

如果jsonStringnil,则会出现一些错误,但您又对错误一无所知,那么您有两种选择:

  1. 将关闭更改为始终返回jsonStringerror
  2. 将错误封装到throwable闭包中。
  3. 第一种情况非常简单,只需更改你的闭包并始终返回错误,如果它是nil则不会出现错误。

    我认为另一种选择更好,如下例所示:

    func alamoRequest(username : String, email: String, password: String, facebook: String, 
                      completion: (inner: () throws -> String) -> ()) {
    
       var jsonValue : JSON?
       let URL = "http://someurl.com/login.php"
       let requestParameters = ["username" : username, "email" : email, "password" : password, "facebook" : facebook, "date": NSNull()];
       var jsonString : String = String()
    
       Alamofire.request(.GET, URL, parameters: requestParameters).validate().responseJSON {
        response in
           switch response.result {
           case .Success:
               if let value = response.result.value {
                   jsonValue = JSON(value)
                   jsonString = jsonValue!["error"].stringValue
                   print("Value in implementation is: \(jsonString)")
                   completionHandler(inner: { return jsonString })
               }
           case .Failure(let error):
               completionHandler(inner: { return error })
           }
       } 
    }
    

    然后您可以通过以下方式调用它:

    self.alamoRequest(usernameField.text!, email: emailField.text!, password: passwordField.text!, facebook: "false") { (inner: () throws -> String) -> Void in
      do {
        let result = try inner()
      } catch let error {
         print(error)
      }
    }
    

    诀窍是alamoRequest函数采用类型为'inner'的名为() throws -> String的附加闭包。这个闭包将提供计算结果,或者它将抛出。闭包本身是在计算过程中通过以下两种方法之一构建的:

    • 如果出现错误:inner: {throw error}
    • 如果成功:inner: {return result}

    我强烈建议您撰写一篇关于在异步调用Using try / catch in Swift with asynchronous closures中使用try/catch的优秀文章

    我希望这对你有所帮助。