Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected non-void return value in void function [duplicate]

Tags:

ios

swift

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 returns I have error Unexpected non-void return value in void function How to fix this?

like image 224
Nikita Zernov Avatar asked May 16 '15 18:05

Nikita Zernov


1 Answers

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.
}
like image 73
Amit89 Avatar answered Oct 14 '22 13:10

Amit89