Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

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


1 Answers

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 {
like image 102
jrturton Avatar answered Nov 20 '25 20:11

jrturton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!