Swift 如何获取childByAutoId()后面的值

Swift 如何获取childByAutoId()后面的值,swift,firebase,firebase-realtime-database,key,Swift,Firebase,Firebase Realtime Database,Key,保存用户创建的项目时,我使用以下代码: let ref = Database.database().reference(fromURL: "https://projectsupport-e475b.firebaseio.com/").child("Projects").childByAutoId() let values = ["ProjectTitle": ProjectTitle, "Fighters": EarlySupporters, "Tags": Tags, "TotalFight

保存用户创建的项目时,我使用以下代码:

let ref = Database.database().reference(fromURL: "https://projectsupport-e475b.firebaseio.com/").child("Projects").childByAutoId() 
let values = ["ProjectTitle": ProjectTitle, "Fighters": EarlySupporters, "Tags": Tags, "TotalFighters": "0", "Description": Description, "ProfileImage": profileImageUrl, "Creator": self.username!] as [String : Any]


ref.updateChildValues(values)
Database.database().reference().child("Projects").observe(.childAdded, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String : AnyObject]
            {
                let project = Project(dictionary: dictionary)
                //The following line was the problem:

                //project.setValuesForKeys(dictionary)


                //I'm still not sure why that solved the problem.

                print(project)
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }

            print(snapshot)
        }, withCancel: nil)
这是我的数据库:

Projects:
        |
        -------KyHzk8VtHitcLlYw1MT
              | 
              - ProjectName: "ProjectNameTest"
              |
              - Creator: "CreatorTest"
              - And so on...
当我尝试检索数据时,我使用以下代码:

let ref = Database.database().reference(fromURL: "https://projectsupport-e475b.firebaseio.com/").child("Projects").childByAutoId() 
let values = ["ProjectTitle": ProjectTitle, "Fighters": EarlySupporters, "Tags": Tags, "TotalFighters": "0", "Description": Description, "ProfileImage": profileImageUrl, "Creator": self.username!] as [String : Any]


ref.updateChildValues(values)
Database.database().reference().child("Projects").observe(.childAdded, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String : AnyObject]
            {
                let project = Project(dictionary: dictionary)
                //The following line was the problem:

                //project.setValuesForKeys(dictionary)


                //I'm still not sure why that solved the problem.

                print(project)
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }

            print(snapshot)
        }, withCancel: nil)
这将导致以下崩溃:

由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不符合密钥创建者的键值编码。”

如何访问数据?我怎么才能拿到钥匙


我明白了!我认为这些代码行存在一些问题:

let project=project(字典:字典)
project.setValuesForKeys(字典)


变量项目的类型是什么?如果前面有
let
关键字,则表示您的
项目
变量是一个不可变类型。设置后,无法修改它。而且,这正是您在下一行使用函数
setValueForKeys
所要做的。尝试使用
var-project=project(dictionary:dictionary)
project
作为可变类型,看看是否有任何区别。

我明白了!我认为这些代码行存在一些问题:

let project=project(字典:字典)
project.setValuesForKeys(字典)


变量项目的类型是什么?如果前面有
let
关键字,则表示您的
项目
变量是一个不可变类型。设置后,无法修改它。而且,这正是您在下一行使用函数
setValueForKeys
所要做的。尝试使用
var project=project(dictionary:dictionary)
project
设置为可变类型,以查看是否有任何差异。

首先在firebase数据库上检查您的值是否按照您的要求正确更新?然后,你能打印你的词汇表,看看它是否能正确检索你的快照值吗?正如人们提到的,我认为你的问题在于你的数据不是firebase,我对此不确定。首先,在你的firebase数据库上,你检查了你的值是否按照你的要求正确更新了吗?然后,你能打印你的字典,看看它是否能正确检索你的快照值吗?正如人们提到的,我认为你的问题在于你的数据不是Firebase我对此不确定。很抱歉,Xcode说变量没有更改,并且显示了警告,此外,我还使用let statite函数保存和获取用户。很抱歉,Xcode表示变量未更改,并显示警告,我还使用let statite函数保存和获取用户。