从json会话返回值以在会话外部使用 [英] return value from json session to use outside of session

查看:106
本文介绍了从json会话返回值以在会话外部使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了如何执行此操作,但是对我来说没有什么意义,而且我做不到.我需要做的就是从json会话中获取数据(如果那就是您所说的话).我大约3周前开始编程,所以我需要外行的条件.我意识到这很可能会被标记为重复项,但是有关此/相关主题的大多数答案都是针对其他语言的,并且我几乎不懂速记,因此它们对我没有多大帮助.

I've searched for how to do this but something just isn't making sense for me and I can't do it. All I need to do is get data out of my json session (if thats what you call it). I started programming about 3 weeks ago so I need lay mans terms please. I realize this is probably going to get marked as a duplicate but most of the answers on this / related topics are for other languages and I barely understand swift so they don't help me much.

我已经花费了数小时来寻找答案,由于我是新手,所以我不知道自己要搜索的内容是否正确.我也尝试阅读iOS开发人员库,但是我不明白它在告诉我什么,或者我找不到合适的部分,因为我仍然无法弄清楚.请尝试解释这一点,而不是让我阅读其他资源.

I've spent hours trying to find the answer and since I'm new I don't know if what I'm searching for is even the right thing to be searching for. I've also tried reading the iOS developer library but either I don't understand what it's telling me or I haven't found the right section because I still can't figure this out. Please try to explain this instead of sending me to read other resources.

这是我的功能

func parseData() {

let urlString = "http://heroesjson.com/heroes.json"
let session = NSURLSession.sharedSession()
let url = NSURL(string: urlString)!

session.dataTaskWithURL(url) { (data: NSData?, response:NSURLResponse?, error: NSError?) -> Void in

    guard let responseData = data else { return }
    var json: [[String: AnyObject]]!

    do {
        json = try NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.AllowFragments) as! [[String: AnyObject]]
    }
    catch {
        //handle error
    }

    var arrayToReturn = [Hero]()
    for element in json {
        let hero = Hero(fromDictionary: element as! [String: AnyObject])
        arrayToReturn.append(hero)
    }
    }.resume()//Closes Session.dataTaskWithURL
} //Closes parseData()

目标是在我的do语句中获取json变量,以便我可以在函数外部解析它或获取我的"arrayToReturn",以便可以将其保存到我使用的全局变量中.

the goal is to get the json variable in my do statement so I can parse it outside of the function or get my "arrayToReturn" so I can save it to a global variable that I use.

如果我理解正确,我不能只将值(arrayToReturn)分配给我的全局变量(heroes),因为这是一个异步请求,因此它只返回nil,因为在请求完成之前已调用了该命令.我想我必须使用完成处理程序或回调函数.我不太了解它们之间的区别,也不了解实现它们的方法位置.

If I understand correctly I can't just assign the value (arrayToReturn) to my global variable (heroes) because this is an asynchronous request so it just returns nil because the command is called before the request is finished. I think I have to use a completion handler or callback function. I don't really understand the difference between them and don't understand how or where to implement them.

此外,我也不是很了解这段代码,我只是知道有必要获取我想要的东西.

Also, I don't understand this code very much either, I just know it's necessary to get what I want.

session.dataTaskWithURL(url) { (data: NSData?, response:NSURLResponse?, error: NSError?) -> Void in

(数据:NSData ?,响应:NSURLResponse ?,错误:NSError?)"看起来像参数,但它们似乎未附加到函数上,因此对我来说没有意义

"(data: NSData?, response:NSURLResponse?, error: NSError?)" looks like parameters but they don't seem to be attached to a function so that doesn't make sense to me

->无效"对我来说没有意义,因为->表示返回任何后续内容,但void向我表明它什么也不返回,那么为什么不将其全部排除在外呢?

"-> Void" Doesn't make sense to me because -> means return whatever follows, but void indicates to me that its returning nothing, so why not just leave it out all together?

->无效 in "这里的含义是什么? /信号是什么意思?

"-> Void in" What what is the significance of in here? what does it mean / signal?

推荐答案

继续阅读

Go and read about Swift Closures. To use a value outside of it, you'd need to pass it to another closure.

func parseData(callback: (heroes: [Hero]) -> Void) {

let urlString = "http://heroesjson.com/heroes.json"
let session = NSURLSession.sharedSession()
let url = NSURL(string: urlString)!

session.dataTaskWithURL(url) { (data: NSData?, response:NSURLResponse?, error: NSError?) -> Void in
    guard let responseData = data else { return }
    var json: [[String: AnyObject]]!

    do {
        json = try NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.AllowFragments) as! [[String: AnyObject]]
    }
    catch {
        //handle error
    }

    var arrayToReturn = [Hero]()
    for element in json {
        let hero = Hero(fromDictionary: element as! [String: AnyObject])
        arrayToReturn.append(hero)
    }

    callback(heroes: arrayToReturn)
    }.resume()//Closes Session.dataTaskWithURL
} //Closes parseData()

您会这样称呼:

parseData { heroes in
    // do something with the array
}

这篇关于从json会话返回值以在会话外部使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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