Iam尝试从ECPLogin中的switch语句返回响应主体,但是它给了我这个错误" void"中出现意外的非void返回值。即使我宣布函数gotourl() - >第33行中的字符串。请建议。
这是我的代码
document.getElementById('date_val').value = user_input.replace(/-/g, "/");
这部分我要返回
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
}
}
}
}
那么有办法吗?感谢
答案 0 :(得分:1)
return
未从gotourl()
返回。它从您传递给ECPLogin
的闭包返回。从您的代码和错误消息中可以看出,您调用的方法将完成闭包作为其最后一个参数,并且此闭包不会返回值(即,它返回Void
)。这就是你得到错误的原因 - 当这个闭包应该什么都不返回时你返回一个字符串。看起来你正在使用的ECPLogin
函数是异步的,这意味着它不会立即产生结果,而是做一些工作并在完成时调用闭包。
您的代码不会返回gotourl()
中的任何内容,这将是与此相关的另一个问题。
如何修复它取决于您的应用需要如何工作。一些选项包括:
gotourl()
,以便完成关闭而不是返回值。然后用调用此闭包的lint替换return
。ECPLogin
调用完成,并以此方式返回值。