ios - Swift:无法使用 NSObjects 将类型 'NSNull' 的值转换为 'NSDictionary'

标签 ios json swift alamofire

Swift 2.1 Xcode 7.1 Alamofire 2.0.2

使用下面描述的 Alamofire 函数,我可以从 API 中检索 JSON 格式的传单列表:

Alamofire.request(Router.GetList()).responseJSON { (_, _, result) in
      var flyers = [Flyer]()

      switch result {
        case .Success(let json):
          if let responseObject = json as? [String: AnyObject], let hits = responseObject["hits"] as? [[String: AnyObject]] {
print(hits)
            for dictionary in hits {
              flyers.append(Flyer(dictionary: dictionary))
            }

            completionHandler(flyers, nil)
          }
        case .Failure(_, let error):
          completionHandler(nil, error as NSError)
      }
    }

print(hits) 的 JSON 响应结果是:

[["title": Code 103, 
 "event": {
    "_id" = 54b0561f274441073751;
    name = "Code 103";
    }
],
["title": Code 104, 
 "event": {
    "_id" = 54b0561f274441073752;
    name = "Code 104";
    }
]]

如下所示,Flyer 类有一个属性(“event”),它引用 Event 类的两个属性:id 和 name:

class Flyer: NSObject{

  var title: String?
  var event: Event?

  init(dictionary: [String: AnyObject]) {
    title    = dictionary["title"] as? String
    event    = Event(dictionary: dictionary["event"] as! [String: AnyObject])
  }
}

class Event: NSObject{

  var id: String?
  var name: String?

  init(dictionary: [String: AnyObject]){

    id              = dictionary["id"] as? String
    name            = dictionary["name"] as? String
  }
}

我收到下一个错误:

Could not cast value of type 'NSNull' (0x107d50378) to 'NSDictionary' (0x107d4fd60).

此错误位于 Flyer 类中的行:

event    = Event(dictionary: dictionary["event"] as! [String: AnyObject])

而且我不知道如何解决这个问题,因为响应不为空。感谢帮助

最佳答案

以下方法在 Playground 中完美运行:尝试进行清理和重建。

let json: AnyObject = [
    "hits": [
        [
            "title": "Code 103",
            "event": [
            "_id": "54b0561f274441073751",
            "name": "Code 103"
            ]
        ],
        [
            "title": "Code 104",
            "event": [
                "_id": "54b0561f274441073752",
                "name": "Code 104"
            ]
        ]
    ]
]

class Flyer: NSObject{

    var title: String?
    var event: Event?

    init(dictionary: [String: AnyObject]) {
        title = dictionary["title"] as? String
        event = Event(dictionary: dictionary["event"] as! [String: AnyObject])
    }
}

class Event: NSObject{

    var id: String?
    var name: String?

    init(dictionary: [String: AnyObject]){

        id = dictionary["id"] as? String
        name = dictionary["name"] as? String
    }
}

var flyers = [Flyer]()

if let responseObject = json as? [String: AnyObject], let hits = responseObject["hits"] as? [[String: AnyObject]] {
    print(hits)
    for dictionary in hits {
        flyers.append(Flyer(dictionary: dictionary))
    }
}

关于ios - Swift:无法使用 NSObjects 将类型 'NSNull' 的值转换为 'NSDictionary',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33455627/

相关文章:

xcode - NSViewController-关闭窗口-Swift-Storyboard

ios - SceneKit 中的背面剔除

java - 将json对象保存到手机中的文件中

java - CodeNameOne ,无需 root 读取 JSON

java - 在jsp页面中调用ajax时加载覆盖

ios - CALayer 只出现一秒钟,然后消失

ios - 如何搜索结构(Swift)

ios - 调用 NSAttributedString.boundingRectWithSize 时应用崩溃

ios - AVExportSession 导出视频超慢

iphone - 在文件名中使用斜杠保存文件 - objective-c