json - 类型转换中的 Firebase Swift 3 错误

标签 json swift firebase firebase-realtime-database swift3

从 Firebase 检索数据时收到一些错误。

这是我的代码:

//  MARK:- Pull in data from Firebase on the main thread. READ ONLY ONCE.
self.ref.child("Power_Stations").child("Stations").observeSingleEvent(of: .value, with: {     snapshot in
if snapshot.exists() {
    for item in snapshot.children {
        let x = item as! FIRDataSnapshot
        let name = x.value(forKey: "Power_Station_Name") as Any
        print(name)
    }
}

})

问题:由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[valueForUndefinedKey:]:此类与键 Power_Station_Name 的键值编码不兼容。” <- 这发生在

let name = x.value(forKey:"Power_Station_name") as Any

上面的错误告诉我当它试图用特定值填充变量时我遇到了崩溃?

但是,当我打印数据时,这是我在日志中收到的:

Snap (-KXRfS0afUet-U8r996p) {
"Power_Station_Address" = nil;
"Power_Station_City" = nil;
"Power_Station_ID" = VsNggVo;
"Power_Station_Inventory" =     {
    android = 12;
    "android_description" = "Fully Stocked";
    ios = 6;
    "ios_description" = "Fully Stocked";
};
"Power_Station_Lat" = "33.6000422";
"Power_Station_Long" = "23.228643";
"Power_Station_Name" = "Power Pods Station";
"Power_Station_Owner" = "Power Pods Inc.";
"Power_Station_Rating" =     {
    "Average_Ranking" = "4.5";
    "Number_Of_Ratings" = 105;
};
"Power_Station_State" = nil;
"Power_Station_Update_Date" = "2016-11-25 18:09:46 +0000";
"Power_Station_Upload_Date" = "2016-11-25 18:09:46 +0000";

下面是我如何将数据保存到数据库。

let newObject = [
                        "Power_Station_ID" : id,
                        "Power_Station_Name" : "Power Pods Station" as String,
                        "Power_Station_Address" : "\(placeMark.thoroughfare)" as String,
                        "Power_Station_City" : "\(placeMark.locality)" as String,
                        "Power_Station_State" : "\(placeMark.administrativeArea)" as String,
                        "Power_Station_Long" : (placeMark.location?.coordinate.longitude)! as Double,
                        "Power_Station_Lat" : (placeMark.location?.coordinate.latitude)! as Double,
                        "Power_Station_Owner" : "Power Pods Inc." as String,
                        "Power_Station_Inventory" : [
                            "ios" : 6,
                            "ios_description" : "Fully Stocked" as String,
                            "android" : 12,
                            "android_description" : "Fully Stocked" as String
                            ] as [String : Any],
                        "Power_Station_Rating" : [
                            "Number_Of_Ratings" : 105,
                            "Average_Ranking" : 4.5,
                            ] as [String : Any],
                        "Power_Station_Upload_Date" : String(describing: NSDate()),
                        "Power_Station_Update_Date" : String(describing: NSDate())
                    ] as [String : Any]

                    self.ref.child("Power_Stations").child("Stations").childByAutoId().setValue(newObject)

谁能帮我找出我做错了什么?

最佳答案

只需添加更多代码,将 snapshot.value 定义为 [String:String](或 String:Any)的字典。该值需要具有该定义,因为它可以是字符串或 NSNumber 等。

let x = item as! FIRDataSnapshot
let dict = x.value! as! [String:String]
let powerStation = dict["Power_Station_Name"]!

关于json - 类型转换中的 Firebase Swift 3 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40810903/

相关文章:

JSON 提供者 jackson 使用 boolean 类型序列化和反序列化的问题

javascript - jquery javascript 从 JSON 对象中删除对象数据

swift - 创建初始值为 0 的信号量会导致执行出现问题

objective-c - 从 swift 调用 Objective-C 宏

firebase - 具有子集合的 Cloud Firestore 架构

java - 日期的 json 表示形式无效

javascript - 将 Json 对象转换为文件对象

swift - 检测 UIScrollView 是否具有滚动高度

firebase - Firestore 是否在离线 30 分钟以上时向我们收费?

具有重写功能的 Firebase 托管不会设置 session cookie