【问题标题】:Cast to NSDictionary returns nil in SwiftCast to NSDictionary 在 Swift 中返回 nil
【发布时间】:2015-03-18 19:14:19
【问题描述】:

我正在尝试在 Swift 中提交 JSON-RPC 请求,并且我正在遵循此 (Perform POST request in iOS Swift) 指南。

不幸的是,我无法通过这样做将答案的“结果”部分转换为 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\"}]";}

【问题讨论】:

  • "result" 不是字典,而是字符串。
  • 正如 Hot Licks 所说,“result”键的值是一个字符串。此字符串是字典的 JSON 表示形式。如果可能,请修复服务器以发送正确的响应。否则,您必须反序列化两次(有关类似问题,请参阅 stackoverflow.com/questions/17283141/…)。
  • 听说你喜欢 JSON

标签: ios swift nsdictionary json-rpc xcode-6.2


【解决方案1】:

正如 Hot Licks 和 Martin R 指出的那样,这确实是服务器端问题。

【讨论】:

    猜你喜欢
    • 2019-01-24
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多