【问题标题】:Firebase Swift 3 Error in Type CastingFirebase Swift 3 类型转换错误
【发布时间】:2016-11-25 18:48:58
【问题描述】:

从 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)

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

【问题讨论】:

    标签: json swift firebase firebase-realtime-database swift3


    【解决方案1】:

    只需添加更多代码,将 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"]!
    

    【讨论】:

    • 谢谢@Jay ...我也使用了以下代码。 x.childSnapshot(forPath: "Power_Station_Address").value 它也有效。你知道为什么或者这是否是现在从 Firebase 中提取数据的官方更改。我以前使用过 firebase,但从未像上面那样从中提取数据。
    • 请记住,在读取 Firebase 时,.values 可以是字符串、数字、另一个字典或可怕的数组(请不要使用数组),所以只有您知道您期望什么,所以您需要定义它应该在代码中。如果答案有效,请不要忘记接受它!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    相关资源
    最近更新 更多