3

I have created one class name Users as below

public class Users: NSObject {
    var name: String?
    var email: String?
}

and other class create array of user class

var regUser = [Users]()

and

func fetchUser(){
    Database.database().reference().child("users").observe(.childAdded, with: {(DataSnapshot) in
        if let dictionary = DataSnapshot.value as? [String: AnyObject]{
            let user = Users()

            user.setValuesForKeys(dictionary)
            self.users.append(user)

            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
            print(user.email)
        }   
    }, withCancel: nil)
}

The following error occurs:

"Users 0x60400028fdc0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name."

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116

1 Answers1

12

Key-value coding depends on Objective-C, and under Swift 4 and the new Objective-C inference rules, your name and email properties will not be available.

You can mark individual properties as @objc:

@objc var name: String?

or you can say that everything in the class is available:

@objcMembers
public class Users: NSObject {
jrturton
  • 118,105
  • 32
  • 252
  • 268