ina patodiya ina patodiya - 3 years ago 205
Swift Question

this class is not key value coding-compliant for the key name.'

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

Answer Source

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 {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download