gpt4 book ai didi

ios - Firestore线程1 : Fatal error when loading data to tableview using real time listener (Swift 4)

转载 作者:行者123 更新时间:2023-11-29 06:02:26 24 4
gpt4 key购买 nike

我正在尝试制作一个财务管理应用程序,需要在表格 View 中显示所有交易。在我更新 Pod 之前,该应用程序运行良好(自从去年圣诞节以来我就没有碰过这个项目,只是回来了)。所以,现在我面临着“线程 1: fatal error :在解包可选值时意外发现 nil”。

我尝试修改我的“结构”,但没有帮助。

这是我的表格 View 中的函数:

func checkUpdates() {
reference(to: .transactions).whereField("timeStamp", isGreaterThan: Date()).addSnapshotListener { (querySnapshot, error) in
guard let snapshot = querySnapshot else { return }
snapshot.documentChanges.forEach {
diff in
if diff.type == .added {
self.transactionArray.append(transactionStruct(dictionary: diff.document.data())!)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
}

我在第 7 行收到线程 1 错误消息,其中显示“self.transactionArray.append...”

下面这些是针对结构的:

protocol DocumentSerializable {
init?(dictionary:[String: Any])
}

struct transactionStruct {
var description: String
var amount: Int
var timeStamp: Date
var dictionary: [String: Any] {
return ["description": description,
"amount": amount,
"timeStamp": timeStamp]
}
}

extension transactionStruct: DocumentSerializable {
init?(dictionary: [String: Any]) {
guard let amount = dictionary["amount"] as? Int,
let description = dictionary["description"] as? String,
let timeStamp = dictionary["timeStamp"] as? Date else { return nil }

self.init(description: description, amount: amount, timeStamp: timeStamp)
}
}

现在,在我添加新条目后,应用程序将崩溃,并且每次都会收到此“线程 1: fatal error ”。为什么会发生这种情况?

最佳答案

任何感叹号都可能导致应用程序崩溃。

TransactionStruct扩展中的初始化程序(请以大写字母开头命名结构)是失败的,因此请使用可选绑定(bind)进行安全解包

if diff.type == .added, let transaction = TransactionStruct(dictionary: diff.document.data()) {
self.transactionArray.append(transaction)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}

如果没有任何反应,则传递的字典已更改。

关于ios - Firestore线程1 : Fatal error when loading data to tableview using real time listener (Swift 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54494158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com