2016-11-25 12 views
0

Firebase에서 데이터를 검색하는 동안 오류가 발생했습니다.Firebase Swift 3 유형 캐스팅 오류

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

내가 뭘 잘못하고 있는지 확인할 수있는 사람이 있습니까?

답변

2

snapshot.value를 [문자열 : 문자열] (또는 문자열 : 모두)의 사전으로 정의하는 코드를 추가하십시오. 그 값은 String, NSNumber 등으로 정의 될 필요가 있습니다.

+0

흥미로운 흥미로운 ....이 작품. –

+0

감사합니다. @ 제이 ... 나는 또한 다음 코드를 사용했습니다. x.childSnapshot (forPath : "Power_Station_Address"). 값 또한 작동했습니다. Firebase에서 데이터를 가져 오기위한 이유가 무엇인지 또는 이것이 공식적인 변경인지 여부를 알고 계십니까? 이전에 firebase를 사용했지만 위와 같이 데이터를 가져 오지 않았습니다. –

+0

Firebase를 읽을 때 .values는 문자열, 숫자, 다른 사전 또는 무시 무시한 배열 (배열을 사용하지 마십시오.) 일 수 있으므로 예상 한 내용 만 알면 코드에 있어야 할 내용을 정의해야합니다. 대답이 효과가 있다면, 그것을 받아들이는 것을 잊지 마십시오! – Jay