0

I'm having this problem, im trying to retrieve data from the firebase database, but it's saying the profilepic isn't a key, but it is. This is the object class.

class User: NSObject {
    var firstname: String?
    var lastname: String?
    var username: String?
    var profilepic: String?
}

and this is me trying to retrieve

func getusers(){
        ref.child("Friends").child(currentuser!).observe(.childAdded, with: { (snapshot) in
            print(snapshot)
            if let dic = snapshot.value as? [String: AnyObject]{
                let user = User()
                user.setValuesForKeys(dic)
                print(user.firstname, user.lastname)

                DispatchQueue.main.async {
                    self.tableview.reloadData()
                }}
        }) { (error) in
            print(error.localizedDescription)
        }

    }

and the crash is here user.setValuesForKeys(dic).

Anyone has any idea why isn't working?

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85

2 Answers2

3

Try this instead:

@objcMembers
class User: NSObject {
    ...
}

The setValuesForKeys API is implemented by the Foundation framework and requires Objective-C compatibility.

The rules for exposing Swift code to Objective-C have changed considerably in Swift 4. Please check the evolution proposal for further information.

User class design. If any of the four properties can be missing from Firebase than your class design might be a good fit. Otherwise, consider declaring the fixed properties as non-optional and creating an initializer instead.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
1

Change your User declaration to this:

class User: NSObject {
    @objc var firstname: String?
    @objc var lastname: String?
    @objc var username: String?
    @objc var profilepic: String?
}

In Swift 4, Objective-C cannot see your instance properties unless you expose them explicitly.

matt
  • 515,959
  • 87
  • 875
  • 1,141