我从我的应用程序向服务器发出 POST 请求,我得到 jsonString 作为响应。我已经制作了将字符串转换为字典的函数,如下所示:
func convertStringToDictionary(text: String) -> [String:AnyObject]? {
if let data = text.dataUsingEncoding(NSUTF8StringEncoding) {
do {
return try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject]
} catch let error as NSError {
print(error)
}
}
return nil
}
从服务器获得响应后,我通过函数将字符串转换为字典,然后我想检查用户是否已登录:
let result = convertStringToDictionary(jsonString as String)
if (result!["loggedIn"] == "1")
{
print("You are logged in!")
}
然后我收到错误“无法将 AnyObject 类型的值转换为预期的参数字符串”。我想如果我想将它与字符串进行比较,我必须将 AnyObject 类型的变量转换为字符串。我已经尝试了我在谷歌上找到的所有选项,但我还没有让它工作。