我試圖從一個url得到一個JSON結果,並檢查它是否等於「OK」,這樣我就可以預製一個segue。我目前收到錯誤: Binary operator '==' cannot be applied to operands of type 'Any' and 'String'
。將JSON結果與字符串比較
這裏是我的代碼:
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print("LightningDB Error")
} else {
if let content = data {
do {
let jsonData = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
if let responseData = jsonData as? NSDictionary {
if let response = responseData["response"] {
if response == "OK" {
performSegue(withIdentifier: "loginSegue", sender: responseString)
} else {
warnField.text = "This user does not exist."
}
}
}
} catch {
}
}
}
}
task.resume()
謝謝!
P.S.做print(response)
就好了。
首先不要使用NSDictionary,使用本地Swift結構數組。 如果您想通過'if if response = responseData [「response」]將其與字符串進行比較,您還需要對String進行響應? String'。 –