I am trying to create HTTP request with Swift2 and to return response outside of nested function. My code looks like this:
let session = NSURLSession.sharedSession()
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? NSHTTPURLResponse
print(httpResponse)
print(data)
// return data
}
I would like to return data variable outside of nested function. Or to have some other variable defined before nested function, which I can set inside of nested function. Like this:
var test = "";
// some nested function
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
test = "test"
})
Anybody has some suggestion for this problem?
dataTaskWithRequestdataTaskWithRequestand just pass that through as thecompletionHandler