I am doing the HTTP request and getting response in my func
. It return the value of response. I am doing it this way:
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
stringResponse = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
if stringResponse == "0" {
return false
} else if stringResponse == "1" {
return true
} else {
return false
}
}
But on all return
s I have error Unexpected non-void return value in void function
How to fix this?
Do this if you want the value after you get response.
func getResponse(completionHandler : ((isResponse : Bool) -> Void)) {
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
stringResponse = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
if stringResponse == "0" {
completionHandler(isResponse : false)
} else if stringResponse == "1" {
completionHandler(isResponse : true)
} else {
completionHandler(isResponse : false)
}
}
}
Now you call it as below from where ever you are calling.
classObject.getResponse {(isResponse) -> Void in
//Do your stuff with isResponse variable.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With