13

我正在尝试编写一个将执行异步 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 新手,堆栈溢出非常有用!

4

1 回答 1

6

让我试着解释一下——这是对线程的误解,一开始我自己也很挣扎。我将尝试简化一些事情。当您运行此代码时:

NSLog("Log 1")

NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
        NSLog("Log in Completion Block")
})

NSLog("Log after request")

您将获得如下所示的输出:

Log 1
Log after request
Log in completion block

让我做一个图表,一种时间线:

                    "Log 1" (before request is sent)
                                       |
                                       |
                           Request sent over Internet
                                       |
                                     /   \  
                   "Log after request"   |
               This is logged *before*   | 
           the other one, because this   |
          one doesn't have to wait for   |
               the network to respond.   |
                                         |
The method finishes and returns a value. |
------------------------------------------------------------------------------
                                         | The network finally responds,
                                         | and the completion block is run.
                                         |

                                         "Log in completion block"

垂直线是方法完成和返回的地方。在所有情况下,您的方法在完成块运行之前已经向其调用者返回了一个值。你不能线性地考虑这个。

我是否应该在这个块中完成这个请求的所有工作,而不是把数据拿出来?

是的,基本上。如果您首先向我展示调用的代码,我可以提供更多帮助getAsynchData()

于 2014-07-17T02:34:05.757 回答