所以我正在开发一个登录到我也在使用alamofire的应用程序。现在,我要做的就是在连接的响应代码为200(成功)时进入一个新视图,然后在响应代码为204(失败)时进入另一个窗口。 。
这是我目前的IBAction
@IBAction func loginBtn(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "login", sender: isvalid200())
self.dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "error", sender: isinvalid())
现在我知道那不是应该怎么做,而是我的想法。另外,这是我的错误处理代码。
func isvalid200(){
Alamofire.request("https://myanimelist.net/api/account/verify_credentials.xml/\(user)/\(pass)")
.validate(statusCode: 200..<200) // if error is 200
.validate(contentType: ["application/json"])
}
func isinvalid() {
Alamofire.request("https://myanimelist.net/api/account/verify_credentials.xml/\(user)/\(pass)")
.validate(statusCode: 204..<204) // if error is 204
.validate(contentType: ["application/json"])
}
至于我得到的错误是这样的
MyAnime [7446:265163]>的窗口不等于“视图的窗口”!
(没有self.dimiss但有它,它确实可以工作,但是它不会停留在基于响应代码IE 200或204的特定窗口上。
任何帮助将是巨大的!
尝试类似这样的操作以打开statusCode
@IBAction func loginBtn(_ sender: Any) {
Alamofire.request("https://myanimelist.net/api/account/verify_credentials.xml/\(user)/\(pass)").responseJSON { [weak self] response in
guard let strongSelf = self, let resp = response.response else { return }
switch resp.statusCode {
case 200:
DispatchQueue.main.async {
strongSelf.performSegue(withIdentifier: "login", sender: strongSelf)
}
case 204:
DispatchQueue.main.async {
strongSelf.performSegue(withIdentifier: "error", sender: strongSelf)
}
default:
//Some other status code
}
}
}
为了对正在发生的事情给出一些解释,让我们将其逐步分解:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句