swift - 使用 swift 忽略 FirebaseDB 中的记录

标签 swift firebase firebase-realtime-database

我正在开发一个快速应用程序,该应用程序将记录从 firebase 显示到 TableView Controller 。

我已经成功提取了所有数据,但我已经在数据库中添加了另一个名为“secretKey”的字段。这不需要在 TableView 上使用,因为它在应用程序的其他地方使用

/image/ENAyM.png

但是现在每次我尝试运行该应用程序时都会出现以下错误

2018-05-07 12:24:24.616490+0100 Loop Guardian[21847:9857567] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSTaggedPointerString 0xa030818c015ea1f9> valueForUndefinedKey:]: this class is not key value coding-compliant for the key createdAt.'
*** First throw call stack:
(0x18204b164 0x181294528 0x18204ae2c 0x1829fe434 0x182944e20 0x182944874 0x18297d930 0x1030084b0 0x103008740 0x1030f2840 0x1044892cc 0x10448928c 0x10448dea0 0x181ff3344 0x181ff0f20 0x181f10c58 0x183dbcf84 0x18b6695c4 0x10301de44 0x181a3056c)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

只有当 secret key 存在于数据库中时才会发生这种情况。如果我将其删除,应用程序就可以正常工作。

我不知何故需要忽略这个字段

这是用于从数据库中提取记录的代码

func observeMessages() {
    let key = UserDefaults.standard.value(forKey: "uid") as! String
    DBrefs.databaseMessages.child(key).observe(.value, with: { (snapshot) in
            if snapshot.exists() {
                let messageData = snapshot.value as! Dictionary<String, AnyObject>
                self.dataArr = []
                for (key,data) in messageData {
                    self.dataArr.append(data)
                }
                //THIS IS WHERE THE ERROR IS THROWN.. THREAD 1: SIGNAL SIGABRT
                self.dataArr = (self.dataArr as NSArray).sortedArray(using: [NSSortDescriptor(key: "createdAt", ascending: true)]) as [AnyObject]
                self.tblMessage.reloadData()
            }
            else {
            }
        })
}

感谢任何帮助

谢谢 奥利弗

最佳答案

for (key,data) in messageData { 
    if (key == "secreyKey") {
       continue;
    }
    self.dataArr.append(data)
}

还使用Swift方法对Array进行排序。不要使用 NSArray,而是使用 Array

如下面的代码:

self.dataArr.sorted(by: {$0["createdAt"] as? Int64 ?? 0 < $1["createdAt"] as? Int64 ?? 0})

关于swift - 使用 swift 忽略 FirebaseDB 中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50213248/

相关文章:

javascript - 从 Javascript 向 Servlet 发送数据的问题

android - 如何在Firebase Android中24小时内每次更新一个值

javascript - 使用身份验证安全规则时 Firebase 读取权限被拒绝

ios - 来自 UIImage 或来自文件的 vImage - Swift 和 Accelerate 框架

ios - 应用程序的 Info.plist 必须包含一个 NSPhotoLibraryUsageDescription 键和一个字符串值,向用户解释应用程序如何使用此数据

swift - 通用 View 生成器无法快速推断类型

firebase - Firebase中基于多个where子句的查询

ios - dyld : Library not loaded: @rpath/Stripe. 框架/Stripe

jquery - 显示 Firebase 存储中的所有图像

ios - Firebase Crashlytics 和 Sentry 不能一起工作? (iOS)