我遇到一个与此标题有关的SIGABRT错误的问题:
Could not cast value of type '__NSDictionaryM' (0x7fff87afce38) to 'NSArray' (0x7fff87afb628)
本质上,我试图从Firestore数据库中检索任务列表。该数据库的结构如下:
lists {
<autoID from Firebase> {
"name": String
"users": [String]
"tasks": [
["name": String, // This is the first task in the array
"description": String,
"due": Date
"delegate": Int
"status": Bool
]
["name": String, // This is the second task in the array
"description": String,
"due": Date
"delegate": Int
"status": Bool
]
]
"completed": [[]] // Possible to have the same structure as "tasks"?
}
}
在我的代码中,我创建了一个查询,该查询试图过滤列表名称和用户的UID。但是,当尝试将tasks
保存到变量中时,SIGABRT会出现说明上述问题。以下是我的代码:
struct Task {
var name: String
var description: String
var due: Date
var delegate: Int
var status: Bool
}
let listsReference = Firestore.firestore().collection("lists")
listsReference.whereField("users", arrayContains: uid).addSnapshotListener { (snapshot, error) in
if let error = error {
// Error exists. errorAlert is created, prompting the user to attempt refreshing their app to load once again.
let errorAlert = UIAlertController(title: "An error occurred.", message: error.localizedDescription, preferredStyle: .alert)
errorAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in }))
self.present(errorAlert, animated: true, completion: nil)
} else {
for document in snapshot!.documents {
let data = document.data()
if data["name"] as! String == self.titleName {
let tasks = data["tasks"] as! [NSDictionary] // SIGABRT error occurs here.
self.listField.text = (data["name"] as! String)
self.documentID = document.documentID
for task in tasks {
let date = task.value(forKey: "due") as! Timestamp
self.dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss +zzzz"
self.dateFormatter.locale = Locale.init(identifier: "en_GB")
self.dateFormatter.dateFormat = "dd MMM yyyy"
self.tasks.append(Task(name: task.value(forKey: "name") as! String, description: task.value(forKey: "description") as! String, due: date.dateValue(), delegate: task.value(forKey: "delegate") as! Int, status: task.value(forKey: "status") as! Bool))
self.tasksTable.reloadData()
}
}
}
}
该如何解决此问题?另外,我可以对此结构进行任何优化(因为我必须找到一种方法来附加到数据库中的此tasks
数组中)。有人知道将来如何更新tasks
数组吗?
所有帮助将不胜感激。谢谢!