问题
Iam trying to return the response body from the switch statment in ECPLogin but it give me this error "unexpected non-void return value in void" even though i declare the function gotourl() -> String in line 33. Please advice.
Here is my code
import UIKit
import SwiftECP
import XCGLogger
class ViewController: UIViewController {
@IBOutlet var UsernameField: UITextField!
@IBOutlet var passwordField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func _Login(_ sender: Any) {
self.gotourl()
// performSegue(withIdentifier: "gotowelcome", sender: self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func gotourl() -> String{
let username: String = UsernameField.text!
let password: String = passwordField.text!
let protectedURL = URL(
string: "https://itsapps.odu.edu/auth/getInfo.p"
)!
let logger = XCGLogger()
logger.setup(level: .debug)
ECPLogin(
protectedURL: protectedURL,
username: username,
password: password,
logger: logger
).start { event in
switch event {
case let .value( body) :
// If the request was successful, the protected resource will
// be available in 'body'. Make sure to implement a mechanism to
// detect authorization timeouts.
print("Response body: \(body)")
return body
// The Shibboleth auth cookie is now stored in the sharedHTTPCookieStorage.
// Attach this cookie to subsequent requests to protected resources.
// You can access the cookie with the following code:
if let cookies = HTTPCookieStorage.shared.cookies {
let shibCookie = cookies.filter { (cookie: HTTPCookie) in
cookie.name.range(of: "shibsession") != nil
}[0]
print(shibCookie)
}
case let .failed(error):
// This is an AnyError that wraps the error thrown.
// This can help diagnose problems with your SP, your IdP, or even this library :)
switch error.cause {
case let ecpError as ECPError:
// Error with ECP
// User-friendly error message
print(ecpError.userMessage)
// Technical/debug error message
print(ecpError.description)
case let alamofireRACError as AlamofireRACError:
// Error with the networking layer
print(alamofireRACError.description)
default:
print("Unknown error!")
print(error)
}
default:
break
}
}
}
}
This part where i want to return
case let .value( body) :
// If the request was successful, the protected resource will
// be available in 'body'. Make sure to implement a mechanism to
// detect authorization timeouts.
print("Response body: \(body)")
return body
So is there a way around it ? thanks
回答1:
That return isn't returning from gotourl(). It's returning from the closure you passed to ECPLogin. From your code and the error message it appears that the method you call takes a completion closure as its last argument, and this closure is not expected to return a value (that is, it returns Void). That's why you get the error-- you're returning a string when this closure is supposed to return nothing. It looks like the ECPLogin function you're using is asynchronous, meaning it doesn't produce a result immediately but instead does some work and calls the closure when it's done.
Your code doesn't return anything from gotourl(), which will be another problem, related to this.
How to fix it depends on how your app needs to work. Some options include:
- Change
gotourl()so that it takes a completion closure instead of returning a value. Then replace yourreturnwith a lint that calls this closure. - Use something like a dispatch group to make your code wait until the
ECPLogincall completes, and return a value that way. - Use some other option so that you don't need to wait until the string is available, like posting a notification.
来源:https://stackoverflow.com/questions/49519232/unexpected-non-void-return-value-in-void-function-swift-4