2

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()
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Omar Kanawati
  • 59
  • 3
  • 7
  • Where do you want to return that data? What needs that value to be returned? What's gonna use it? Starting from these we can start understanding what can be done. – Danny S Jan 14 '15 at 22:39
  • http://stackoverflow.com/questions/27650322/array-of-multiple-urls-nsfilemanager-swift/27650412#27650412 – Leo Dabus Jan 14 '15 at 22:52

1 Answers1

4

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()
}
Antonio
  • 71,651
  • 11
  • 148
  • 165