Alamofire要求为零 [英] Alamofire request coming up nil

查看:85
本文介绍了Alamofire要求为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用WebServices的iOS应用,我发现Alamofire非常适合我的工作,但是我遇到了问题;该应用程序要求用户登录这是Alamofire呼叫,并且可以正常运行。

I'm developing an iOS app which user WebServices and I find Alamofire just perfect for what I'm doing but I'm having a problem; the app asks the user to login which is an Alamofire call and does it just fine.

问题是,它必须根据另一个视图的内容创建一个集合视图Alamofire请求,但始终为 nil

The problem is, it has to create a collection view based on the content of another Alamofire request but is always nil.

    func getJSON(URLToRequest: String) -> JSON {

    let comp:String = (prefs.valueForKey("COMPANY") as? String)!
    let params = ["company":comp]
    var json:JSON!

    let request = Alamofire.request(.POST, URLToRequest, parameters: params).responseJSON {
        response in
        switch response.result {
        case .Success:
            if let value = response.result.value {
                json = JSON(value)
            }
        default:
            json = JSON("");

        }
    }
    debugPrint(request.response)

    return json;
}

相同的代码块非常适合登录,但在这种情况下不能使用BTW调试打印始终打印 nil

The same codeblock works perfect for the Login but doesn't in this case BTW the debug Print always print nil

推荐答案

您正在尝试设置 request.response 之前,请记住Alamofire 异步工作,因此您必须使用闭包返回JSON,但请记住,Alamofire也返回错误,因此我强烈建议改用以下代码:

You're trying to access to request.response before it has been set, remember that Alamofire works asynchronously, so you have to return in your case the JSON using closures, but remember that Alamofire also returns an error, so I strongly recommend use the following code instead:

func getJSON(URLToRequest: String, completionHandler: (inner: () throws -> JSON?) -> ()) {

   let comp:String = (prefs.valueForKey("COMPANY") as? String)!
   let params = ["company":comp]

   let request = Alamofire.request(.POST, URLToRequest, parameters: params).responseJSON {
    response in

    // JSON to return
    var json : JSON?

    switch response.result {
    case .Success:

        if let value = response.result.value {
            json = JSON(value)
        }

        completionHandler(inner: { return json })

    case .Failure(let error):
        completionHandler(inner: { throw error })
    }
} 

诀窍在于 getJSON 函数需要一个名为()的'inner'附加闭包--> JSON?。这个闭包要么提供计算结果,要么抛出。闭包本身是通过以下两种方式之一构造的:

The trick is that the getJSON function takes an additional closure called 'inner' of the type () throws -> JSON?. This closure will either provide the result of the computation, or it will throw. The closure itself is being constructed during the computation by one of two means:


  • 如果出现错误: inner :{throw error}

  • 成功的情况下: inner:{return json}

  • In case of an error: inner: {throw error}
  • In case of success: inner: {return json}

然后您可以这样称呼它:

And then you can call it like in this way:

self.getJSON("urlTORequest") { (inner: () throws -> JSON?) -> Void in
  do {
     let result = try inner()
  } catch let error {
     print(error)
  }
}

希望对您有所帮助。

这篇关于Alamofire要求为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆