在 Swift 中投射到 NSDictionary returns nil
Cast to NSDictionary returns nil in Swift
我正在尝试在 Swift 中提交 JSON-RPC 请求,并且我正在遵循此 (Perform POST request in iOS Swift) 指南。
不幸的是,我无法通过这样做将答案的 "result" 部分投射到 NSDictionary:
if let responseDictionary = responseObject as? NSDictionary {
if let errorDictionary = responseDictionary["error"] as? NSDictionary {
println("error logging in (bad userid/password?): \(errorDictionary)")
} else if let resultDictionary = responseDictionary["result"] as? NSDictionary {
println("successfully logged in, refer to resultDictionary for details: \(resultDictionary)")
} else {
println("we should never get here")
println("responseObject = \(responseObject)")
}
}
其中 responseObject 是 AnyObject。
我的响应对象如下所示:
{
id = 1;
jsonrpc = "2.0";
result = "[{\"ID\":11,\"Name\":\"MyName\",\"LLogon\":\"2015-03-16T13:04:14\"}]";}
正如 Hot Licks 和 Martin R 所指出的,这确实是一个服务器端问题。
我正在尝试在 Swift 中提交 JSON-RPC 请求,并且我正在遵循此 (Perform POST request in iOS Swift) 指南。
不幸的是,我无法通过这样做将答案的 "result" 部分投射到 NSDictionary:
if let responseDictionary = responseObject as? NSDictionary {
if let errorDictionary = responseDictionary["error"] as? NSDictionary {
println("error logging in (bad userid/password?): \(errorDictionary)")
} else if let resultDictionary = responseDictionary["result"] as? NSDictionary {
println("successfully logged in, refer to resultDictionary for details: \(resultDictionary)")
} else {
println("we should never get here")
println("responseObject = \(responseObject)")
}
}
其中 responseObject 是 AnyObject。
我的响应对象如下所示:
{
id = 1;
jsonrpc = "2.0";
result = "[{\"ID\":11,\"Name\":\"MyName\",\"LLogon\":\"2015-03-16T13:04:14\"}]";}
正如 Hot Licks 和 Martin R 所指出的,这确实是一个服务器端问题。