AFNetworking和Swift-保存json响应 [英] AFNetworking and Swift - Save json response

查看:102
本文介绍了AFNetworking和Swift-保存json响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想迅速发出GET请求以获取一些Json数据. 我尝试使用AFNetworking可以正常工作,但是我不知道如何返回我得到的Json.

I want to make a GET request in swift to get some Json data. I tried to use AFNetworking and it works, but I don't know how to return the Json I get.

我尝试了return,但是它是在GET之前完成的,所以我什么也没得到...

I tried with a return but it's made before the GET so I get nothing...

func makeGet(place:String) -> String
{
    var str:String = ""
    let manager = AFHTTPRequestOperationManager()
    manager.requestSerializer.setValue("608c6c08443c6d933576b90966b727358d0066b4", forHTTPHeaderField: "X-Auth-Token")
    manager.GET("http://something.com/api/\(place)",
        parameters: nil,
        success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
            str = "JSON:  \(responseObject.description)"
            println(str) //print the good thing
        },
        failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
            str = "Error: \(error.localizedDescription)"
        })
    return str //return ""
}

你能帮我吗?

推荐答案

您不会从该函数获得响应,因为GET操作是异步发生的.也就是说,执行顺序如下:

You're not getting a response from that function, because the GET operation happens asynchronously. That is, the order of execution looks like this:

  1. 您致电makeGet

makeGet创建manager,这会触发GET请求

makeGet creates manager, which fires off a GET request

makeGet完成执行并返回一个空字符串

makeGet finishes executing and returns an empty string

(稍后) manager从服务器接收一个值,并执行successfailure块.

(some time later) manager receives a value back from the server and executes either the success or failure block.

因此,唯一可以访问从服务器返回的JSON的步骤是在步骤4中,并且您需要找到一种存储该值的方法,以便可以解析或使用它.这里有多种选择-一种是定义闭包,以在类实例上调用事件处理程序,如下所示:

So the only time you have access to the JSON that comes back from the server is in step 4, and you need to find a way of storing that value so you can parse it or use it or whatever. There are a variety of options here -- one is to define closures that call event handlers on your class instance, like this:

class MyClass {

    func jsonLoaded(json: String) {
        println("JSON: \(json)")
    }

    func jsonFailed(error: NSError) {
        println("Error: \(error.localizedDescription)")
    }

    func makeGet(place:String) {        
        let manager = AFHTTPRequestOperationManager()
        manager.requestSerializer.setValue("608c6c08443c6d933576b90966b727358d0066b4", forHTTPHeaderField: "X-Auth-Token")
        manager.GET("http://something.com/api/\(place)",
            parameters: nil,
            success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
                self.jsonLoaded(responseObject.description)
            },
            failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in
                self.jsonFailed(error)
            }
        )
    }

}

这篇关于AFNetworking和Swift-保存json响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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