2017-08-16 39 views
0

我希望你能幫助我: 我嘗試從firebase數據庫中將userdata提取到用戶類中並調用「setValuesForKeys」。我明白,我的類屬性必須與firebase字典中的屬性完全相同,但是我得到了錯誤「此類不是關鍵城市的關鍵值編碼。」想從firebase中將userdata提取到字典中

func fetchUsers(){ 
    Database.database().reference().child("users").observe(.childAdded, with: { (snapshot) in 
     if let usersDictionary = snapshot.value as? [String: String] { 
      let users = Userdata() 
      users.setValuesForKeys(usersDictionary) 
     } 
    }, withCancel: nil) 
} 

我的用戶類是

class Userdata: NSObject { 
    var email: String? 
    var password: String? 
    var firstname: String? 
    var lastname: String? 
    var street: String? 
    var streetno: String? 
    var zipcode: String? 
    var city: String? 
    var phone: String? } 

從火力點快照看起來像

Snap (ndLBXXX75Oe9Y1PXrqfISL8A4v82) { 
city = Washington; 
email = "[email protected]"; 
firstname = Andre; 
lastname = Doe; 
password = xxxxxx; 
phone = ""; 
street = "Mainstreet"; 
streetno = 1; 
zipcode = 11111; 

}

,並從數據庫字典看起來像

["city": Washington, "firstname": Andre, "lastname": Doe, "email": [email protected], "password": xxxxxx, "streetno": 1, "phone": , "street": Mainstreet, "zipcode": 11111] 

我有一個解決方案迄今使用:

users.city = dictionary["city"] 

我的問題/問題:我想了解錯誤信息的問題「這一類不是鍵值編碼兼容的關鍵城市「。因爲在課堂和firebase快照中的關鍵看起來是一樣的。

+0

這個錯誤是因爲你試圖使用KVC。閱讀有關KVC的使用方法,請訪問https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/。字典沒有關鍵城市,電子郵件的屬性...這就是爲什麼你看到錯誤。你可以遍歷字典中的所有鍵並使用user.setValue(dictionary [key],forKey:key) –

+0

查看我的答案,使快照轉換爲自定義類很簡單 - https://stackoverflow.com/a/38154998/2019221 – Callam

+0

@StasVolskiy感謝您的答案和您的解決方案。它確實有效。我的解決方案是: 'user.firstname = usersDictionary [「firstname」]' 我已閱讀此https://developer.apple.com/documentation/objectivec/nsobject/1417515-setvaluesforkeys,我認爲我可以採取dictonary-keys並將它們與我的類屬性相匹配。 –

回答

0

工作解決方案:我不得不擴展我的用戶類。現在,整個用戶類的樣子:

下面是代碼:

import Foundation 

    class UserData: NSObject { 
     var id: String? 
     var email: String? 
     var password: String? 
     var salutation: String? 
     var degree: String? 
     var firstname: String? 
     var lastname: String? 
     var street: String? 
     var streetno: String? 
     var zipcode: String? 
     var city: String? 
     var phone: String? 
     var profileImage: String? 

    init(dictionary: [String: Any]) { 
     self.city = dictionary["city"] as? String 
     self.id = dictionary["id"] as? String 
     self.email = dictionary["email"] as? String 
     self.salutation = dictionary["salutation"] as? String 
     self.degree = dictionary["degree"] as? String 
     self.firstname = dictionary["firstname"] as? String 
     self.lastname = dictionary["lastname"] as? String 
     self.password = dictionary["password"] as? String 
     self.phone = dictionary["phone"] as? String 
     self.street = dictionary["street"] as? String 
     self.streetno = dictionary["streetno"] as? String 
     self.zipcode = dictionary["zipcode"] as? String 
     self.profileImage = dictionary["profileImage"] as? String 

    } 
}