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?
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.