Ios 在Swift 4中使用Alamofire从URL检索数据
我正在尝试使用Alamofire向包含JSON的URL发送请求,如下所示:Ios 在Swift 4中使用Alamofire从URL检索数据,ios,json,swift,alamofire,Ios,Json,Swift,Alamofire,我正在尝试使用Alamofire向包含JSON的URL发送请求,如下所示: { "code": 11, "responseTS": 1587219531266, "message": "not running", "version": "2.1", "command": "http://www.url.com", "status": "NotRunning" } 这是我的职责。现在,它打印响应,但停止并没有完成其余的代码。因为JSON中的消息“未运行”,所以它应该调用g
{
"code": 11,
"responseTS": 1587219531266,
"message": "not running",
"version": "2.1",
"command": "http://www.url.com",
"status": "NotRunning"
}
这是我的职责。现在,它打印响应,但停止并没有完成其余的代码。因为JSON中的消息“未运行”,所以它应该调用getDBLocation(),但在打印响应后(在guard语句之前)停止。有人能告诉我为什么会发生这种情况吗
XCode没有给出任何错误,应用程序也没有崩溃,只是没有像我期望的那样继续功能
func checkStatus() {
let url = qpeUrl
Alamofire.request(url).responseJSON { (response) in
print(response)
print(response.result)
guard response.result.error == nil else {
print("error")
print(response.result.error!)
return
}
guard let tagStatus = response.result.value as? [String: Any] else {
self.audioFail.play()
let alertConn = UIAlertController(title: "ERROR!", message:
"Couldn't connect", preferredStyle: .alert)
alertConn.addAction(UIAlertAction(title: "完了", style: .cancel, handler: nil))
self.present(alertConn, animated: true, completion: nil)
self.captureSession.startRunning()
return
}
if let tagstatus = tagStatus["message"] as? String {
if tagstatus.contains("Unknown") || tagstatus.contains("not running"){
self.getDBLocation()
}
}
if let tagstatus = tagStatus["message"] as? String {
if tagstatus.contains("TagPosition") {
self.audioSuccess.play()
self.sendQPERequest()
}
}
}
}
很抱歉问了一个以前可能有人问过的问题。我想我遗漏了一些明显的东西,因为它以前是有效的。然而,在尝试修复了几个星期后,我真的不知道发生了什么
编辑:代码似乎运行正常,只有我一步一步地完成它。如果我不单步执行,它将打印SUCCESS(print(response.result)),然后在第一个guard语句之前停止。代码可以通过单步执行,所以我真的不知道发生了什么。您希望代码做什么?您是否仔细查看了代码并了解了可能发生的情况?请您使用此
响应检查响应结果是否成功。结果
?对不起,我更新了我的问题@koen。当我单步执行时,它打印JSON响应,然后停止,没有错误。我检查了它,response.result成功。这是一个GET请求。我试图检查JSON中“message”的值@FaysalAhmedUnrelated,如果让tagstatus=tagstatus[“message”]as,我建议不要使用多个?字符串{…}
,但要这样做一次(或者使用guard let tagstatus=tagstatus[“message”]作为字符串,否则{return}
模式,同样,只做一次)。