Michael Westbrooks II Michael Westbrooks II - 11 days ago 9
JSON Question

Firebase Swift 3 Error in Type Casting

Receiving some errors while retrieving data from Firebase.

Here is my code:

// 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)
}
}


})

Problem: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key Power_Station_Name.' <- This is happening at

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


The above error is telling me that i'm getting an crash when it is attempting to fill a variable with a particular value?

However, when I print the data this is what i'm receiving in the logs:

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";


}

Below is how i am saving data to the database.

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)


Can anyone help me identify what I am doing wrong?

Jay Jay
Answer

Just add a little more code that defines the snapshot.value as a Dictionary of [String:String] (or String:Any). The value needs to have that definition as it could be a String, or NSNumber etc.

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