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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…