0

I hope you could help me: I try to fetch userdata from a firebase database into a user class and call "setValuesForKeys". I do understand that my class properties have to be exactly the same as in the firebase dictionary, but I've got the error "this class is not key value coding-compliant for the key city."

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

And my user class is

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?  }

The snapshot from firebase looks like

Snap (ndLBXXX75Oe9Y1PXrqfISL8A4v82) {
city = Washington;
email = "1@a.com";
firstname = Andre;
lastname = Doe;
password = xxxxxx;
phone = "";
street = "Mainstreet";
streetno = 1;
zipcode = 11111;

}

And the dictionary from the database looks like

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

I have a solution so far by using:

users.city = dictionary["city"]

My question / problem: I do want to understand the problem behind the error message "this class is not key value coding-compliant for the key city." because the key at the class and in the firebase snapshot looks like the same.

KENdi
  • 7,576
  • 2
  • 16
  • 31
andre_hold
  • 562
  • 8
  • 20
  • This error is because you are trying to use KVC. Read about KVC usage here https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/. Dictionary doesn't have properties for keys city, email... That's why you see the error. You can loop through all keys from dictionary and use user.setValue(dictionary[key], forKey: key) – Stas Volskiy Aug 16 '17 at 15:09
  • Check out an answer of mine that makes converting snapshots to custom classes easy - https://stackoverflow.com/a/38154998/2019221 – Callam Aug 16 '17 at 20:55
  • @StasVolskiy thanks for your answer and your solution with a loop. It does work. My solution is: `user.firstname = usersDictionary["firstname"]` I've read this https://developer.apple.com/documentation/objectivec/nsobject/1417515-setvaluesforkeys and i thought that i could take the dictonary-keys and match them with my class properties. – andre_hold Aug 17 '17 at 11:40

1 Answers1

0

Working solution: I had to extend my user class. Now, the whole user class looks like:

Here is the code :

 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

    }
}
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
andre_hold
  • 562
  • 8
  • 20