【发布时间】:2018-03-27 17:39:43
【问题描述】:
我试图从 ECPLogin 中的 switch 语句返回响应正文,但它给了我这个错误“unexpected non-void return value in void”,即使我在第 33 行声明了函数 gotourl() -> String。请指教.
这是我的代码
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
}
}
}
}
我要返回的这部分
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
那么有办法解决吗?谢谢
【问题讨论】: