void函数中出现意外的非void返回值 [英] Unexpected non-void return value in void function
本文介绍了void函数中出现意外的非void返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在执行HTTP请求并在 func
中获得响应。它返回响应的值。我是这样做的:
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
}
}
但是在所有上返回
s我有错误 void函数中出现意外的非void返回值
如何解决这个问题?
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.
}
这篇关于void函数中出现意外的非void返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文