ios - 此类与键 XXX 的键值编码不兼容 - 类是否正确连接?

标签 ios swift firebase firebase-realtime-database

首先,这不是 What does this mean? "'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X" 的副本

我已经查看了那个问题和其他几个问题,并确保我的类(class)通过身份检查器和连接检查器进行连接。我的类(class)也通过助理编辑器中的自动模式显示。

    if let currentUser = FIRAuth.auth()?.currentUser {

        ref = FIRDatabase.database().reference()
        ref.child("Teams").child(self.teamName.text!).setValue(["Name" : self.teamName.text!])
        ref.child("Teams").child(self.teamName.text!).setValue(["Number" : self.teamNumber.text!])
        ref.child("Teams").child(self.teamName.text!).setValue(["Password" : self.teamPassword.text!])
        ref.child("Teams").child(self.teamName.text!).setValue(["memberCount" : 1])
        print("1")

        let userName = "member" + String(1)
        let currentUserEmail = currentUser.uid
        ref.child("Teams").child(self.teamName.text!).child("memberList").setValue([userName : currentUserEmail])
        print("2")

        if let userteamcount = self.ref.child("Users").child(currentUser.uid).value(forKey: "teamCount") as? Int {
            let currentTeam = "team" + String(userteamcount + 1)
            print("4")
            self.ref.child("Users").child(currentUser.uid).setValue(["teamCount" : (userteamcount + 1)])
            print("5")
            self.ref.child("Users").child(currentUser.uid).child("joinedTeams").setValue([currentTeam : self.teamNumber.text!])
            print("6")
        }
    }

一个重要的注意事项是它在发送错误之前打印出 1 和 2。

此外,这是我得到的特定错误:

1
2
Terminating app due to uncaught exception 'NSUnknownKeyException',    reason: 
'[<FIRDatabaseReference 0x618000054160> valueForUndefinedKey:]: 
this class is not key value coding-compliant for the key teamCount.'

非常感谢。

最佳答案

self.ref.child("Users").child(currentUser.uid).value(forKey: "teamCount") as? Int

您不能像那样从数据库中获取值。您需要观察引用以获取值。但是看起来您正在尝试增加一个值,因此无论如何您都应该使用事务来执行此操作。因为如果不这样做,当两个用户同时尝试增加它时,一个人的更改很可能会覆盖另一个人(就我个人而言,我发现它也更具可读性)。方法如下。

self.ref.child("Users").child(currentUser.uid).child("teamCount").runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in
  var teamCount = currentData.value as? Int
  if teamCount != nil {
    currentData.value = teamCount + 1
  }
  return FIRTransactionResult.success(withValue: currentData)
}) { (error, committed, snapshot) in
     if error == nil {
       // Update joinedTeams here
       let value = snapshot.value as? Int
       let currentTeam = "team\(value)"
       self.ref.child("Users").child(currentUser.uid).child("joinedTeams").child(currentTeam).setValue(self.teamNumber.text!)
     }
}

下面是您通常如何从您的数据库中加载 teamCount,而无需事务。

self.ref.child("Users").child(currentUser.uid).child("teamCount").observeSingleEvent(.value, with: { dataSnapshot in
  let teamCount = dataSnapshot.value as? Int
})

我还没有测试代码,但我认为它应该可以工作。

链接:

Firebase Transactions

关于ios - 此类与键 XXX 的键值编码不兼容 - 类是否正确连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42703886/

相关文章:

flutter - Flutter 中的 Firebase 后台消息处理程序产生重复的隔离并运行应用程序两次

java - Firebase 实时数据库 Android : How to Display Data, 一次部分

ios - Xcode 5 无法识别 Storyboard中的自定义 UIViewController 子类

javascript - 如何在不同的 Firebase 文档上记录来自与 DialogFlow 机器人的不同对话的响应?

ios - 带有图像的 UIScrollView,图像宽度不正确

swift - Watchos 应用程序几天后会自动卸载

ios - 设置约束后 subview 无法显示

ios - SwiftUI:关闭所有事件的工作 TableView

iphone - UITable View 到详细 View 使用 AFNetworking 解析 JSON

ios - 使用转换(_ :to:) method in swift