我正在尝试编写一个函数来执行异步 GET 请求,并返回响应(作为任何数据类型,但这里是 NSData)。
这个问题是基于:如何快速使用 NSURLConnectioncompletionHandler https://stackoverflow.com/questions/24016714/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
我看过:如何在 Swift 中使用带有 return 的completionHandler闭包? https://stackoverflow.com/questions/24647406/how-to-use-completionhandler-closure-with-return-in-swift但这并不能真正回答我的问题。我的目标是从块中获取异步请求的数据,以便在代码的其他地方使用。我是否应该在这个块中完成此请求的所有工作而不取出数据?
谢谢你!
EDIT
好吧,我有一个我认为可能可行的选择,但对我来说似乎并不合适。有人可以告诉我这是否是实现我的目标的最佳方式?
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 新手,堆栈溢出非常有帮助!