Return object for a method inside completion block

2019-06-01 03:26发布

I want to make a method with URL parameter that returns the response of calling that URL. How can I return the data obtained inside a completion block for a method?

class func MakeGetRequest(urlString: String) -> (data: NSData, error: NSError)
{
    let url = NSURL(string: urlString)
    var dataResponse: NSData
    var err: NSError

    let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
           //How can I return the data obtained here....
    })

    task.resume()
}

1条回答
Ridiculous、
2楼-- · 2019-06-01 03:41

If you want the MakeGetRequest method to return data obtained via dataTaskWithURL, you can't. That method performs an asynchronous call, which is most likely completed after the MakeGetRequest has already returned - but more generally it cannot be know in a deterministic way.

Usually asynchronous operations are handled via closures - rather than your method returning the data, you pass a closure to it, accepting the parameters which are returned in your version of the code - from the closure invoked at completion of dataTaskWithURL, you call that completion handler closure, providing the proper parameters:

class func MakeGetRequest(urlString: String, completionHandler: (data: NSData, error: NSError) -> Void) -> Void
{
    let url = NSURL(string: urlString)
    var dataResponse: NSData
    var err: NSError

    let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
        completionHandler(data: data, error: error)
    })

    task.resume()
}

Swift 5 update:

class func makeGetRequest(urlString: String, completionHandler: @escaping (Data?, Error?) -> Void) -> Void {
    let url = URL(string: urlString)!
    var dataResponse: Data
    var err: NSError

    let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, respone, error) -> Void in
        completionHandler(data, error)
    })

    task.resume()
}
查看更多
登录 后发表回答