在我的Swift 4代码中调用setValuesForKeys失败 [英] Calling setValuesForKeys fails in my Swift 4 code

查看:194
本文介绍了在我的Swift 4代码中调用setValuesForKeys失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个问题,我试图从firebase数据库中检索数据,但这是说profilepic不是关键,而是关键.这是对象类.

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?
}

这是我要检索的

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)
        }

    }

崩溃在这里user.setValuesForKeys(dic).

任何人都知道为什么不工作吗?

Anyone has any idea why isn't working?

推荐答案

尝试以下方法:

@objcMembers
class User: NSObject {
    ...
}

setValuesForKeys API由Foundation框架实现,并且需要与Objective-C兼容.

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

在Swift 4中,将Swift代码暴露给Objective-C的规则已经发生了很大变化.请

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

用户类别设计.如果Firebase可能缺少这四个属性中的任何一个,那么您的类设计可能是一个不错的选择.否则,请考虑将固定属性声明为非可选,然后创建一个初始化程序.

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.

这篇关于在我的Swift 4代码中调用setValuesForKeys失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆