cI正在尝试使用Swift 2和Alamofire向API发出请求,然后存储数据,以便我可以在另一个函数中使用它。
例如,这是我下面的代码,我正试图找出如何将其分配给变量,以便以后在文件中使用它。我尝试过谷歌搜索,尝试了很多选择,但在过去的3个小时里我什么都没有。

func getToken() {
    Alamofire.request(.GET, "https://somesite.com/ping").responseJSON { response in
        print(response)
    }
}

如我们所见,我们有响应,但是我们可以把它的内容赋给一个变量,这样我们就可以在函数中使用它,比如pryResponse
func pryResponse() {
    print(response)
}

类似的东西,但实际的变量。

最佳答案

试试这个。。随着最新版本的发布,语法发生了一些变化。

Alamofire.request(.GET, "https://livewx.tv/ping").responseJSON(){
                (_, json, result) in
                switch result {

                case .Success:
                    let json = result.value!
                    let stuff = json["results"] as! [[String: AnyObject]]
                    //stuff now holds a dictionary with the json
                case .Failure(_,  _):
                    //Error
                }
            }

10-05 17:49
查看更多