void函数中出现意外的非void返回值[重复]

时间:2022-09-10 23:51:43

This question already has an answer here:

这个问题在这里已有答案:

I am doing the HTTP request and getting response in my func. It return the value of response. I am doing it this way:

我正在执行HTTP请求并在我的func中获得响应。它返回响应的值。我是这样做的:

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?

但是在所有返回时我都有错误void函数中出现意外的非void返回值如何解决这个问题?

2 个解决方案

#1


17  

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.
}

#2


0  

The last parameter of sendAsynchronousRequest is a completion handler with type (NSURLResponse!, NSData!, NSError!) -> Void. Note it doesn't have any return type, so don't return anything.

sendAsynchronousRequest的最后一个参数是一个带有类型的完成处理程序(NSURLResponse!,NSData!,NSError!) - > Void。请注意,它没有任何返回类型,因此不要返回任何内容。

The completion handler is called by the connection framework, it doesn't need any return type.

完成处理程序由连接框架调用,它不需要任何返回类型。

It's pretty obscure what you are trying to achieve by that returns but you can't do it this way.

你想要通过回报实现的目标非常模糊,但你不能这样做。

#1


17  

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.
}

#2


0  

The last parameter of sendAsynchronousRequest is a completion handler with type (NSURLResponse!, NSData!, NSError!) -> Void. Note it doesn't have any return type, so don't return anything.

sendAsynchronousRequest的最后一个参数是一个带有类型的完成处理程序(NSURLResponse!,NSData!,NSError!) - > Void。请注意,它没有任何返回类型,因此不要返回任何内容。

The completion handler is called by the connection framework, it doesn't need any return type.

完成处理程序由连接框架调用,它不需要任何返回类型。

It's pretty obscure what you are trying to achieve by that returns but you can't do it this way.

你想要通过回报实现的目标非常模糊,但你不能这样做。