我正在尝试编写一个将执行异步 GET 请求并返回响应的函数(作为任何数据类型,但这里是作为 NSData)。
这个问题基于:How to use NSURLConnection completionHandler with swift
func getAsynchData() -> NSData {
var dataOutput : NSData
let url:NSURL = NSURL(string:"some url")
let request:NSURLRequest = NSURLRequest(URL:url)
let queue:NSOperationQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
/* this next line gives the below error */
dataOutput = data
})
return dataOutput
}
但我收到一个错误:
error: variable 'dataOutput' captured by a closure before being initialized
我已经尝试从completionHandler返回值,但它需要一个void返回,这让我想起了我希望在没有帮助的情况下解决这个问题......:D
我看过: How to use completionHandler Closure with return in Swift? 但这并不能真正回答我的问题。我的目标是从我的异步请求中获取数据,以便在我的代码中的其他地方使用。我是否应该在这个块中完成这个请求的所有工作,而不是把数据拿出来?
谢谢!
编辑
好的,所以我有一个我认为可能有用的选项,但对我来说似乎不合适。有人能告诉我这是否是实现我目标的最佳方式吗?
func doThingsWithData( data: NSData ) -> String {
/* code to extract string from NSData */
return somestring
}
func getAsynchData() {
let url:NSURL = NSURL(string:"some url")
let request:NSURLRequest = NSURLRequest(URL:url)
let queue:NSOperationQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
/* this next line gives the below error */
doThingsWithData(data)
})
}
编辑 2 -> 响应撤消
谢谢,撤消。你的回答对我来说很有意义。这里有更多的谜题。我有一个类是我的 API 处理程序。我希望能够实例化该类,并在其上调用一个函数以从 API 获取数据。我宁愿通过一个 api 调用获取所有数据,而不是每次为我需要输出的每个值都进行单独调用,因为单个 API 调用包含我需要的所有数据,但这可能是另一个答案。这是代码:
class GetInfoFromAPI {
func getSpecificValue(index : String) -> String {
/* I assume I need to send the values from this function, yea? but how do I get them here? */
}
func doThingsWithData( data: NSData ) -> String {
/* code to extract string from NSData */
var error: NSError?
let jsonDict = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) as NSDictionary
specificValue1 : String = jsonDict.valueForKey("value1") as String
specificValue2 : String = jsonDict.valueForKey("value2") as String
specificValue3 : String = jsonDict.valueForKey("value3") as String
/* I want to get these ^^^ values into the ViewController below */
}
func getAsynchData() {
let url:NSURL = NSURL(string:"some url")
let request:NSURLRequest = NSURLRequest(URL:url)
let queue:NSOperationQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
/* this next line gives the below error */
doThingsWithData(data)
})
}
}
class ViewController: UIViewController {
@IBOutlet var labelVariable1: UILabel
@IBOutlet var labelVariable2: UILabel
@IBOutlet var labelVariable3: UILabel
let apiInstance = GetInfoFromAPI()
@IBAction func buttonTapped(sender : AnyObject) {
labelVariable1 = apiInstance.getSpecificValue(1)
labelVariable2 = apiInstance.getSpecificValue(2)
labelVariable3 = apiInstance.getSpecificValue(3)
}
}
感谢您花时间回答我的问题。我是 swift 新手,堆栈溢出非常有用!