Vul*_*lps 5 ios swift google-cloud-firestore
令人惊讶的是,在 Stack Overflow 搜索中输入此错误不会返回实际提到此错误 0.0 的结果
所以Event我的 Swift IOS 应用程序中有一个对象,它在 Firestore 上保存为文档,如下所示
和start字段end是时间戳。
在 xcode 上,当Event查询集合时,结果将Event使用此初始化程序解码为 s
init(document: DocumentSnapshot) {
self.id = document.documentID
let d = document.data()
self.title = d!["title"] as? String
let stamp = d!["start"] as? Timestamp
let estamp = d!["end"] as? Timestamp
self.start = stamp?.dateValue()
self.end = estamp?.dateValue()
/*
There is a breakpoint here!
*/
self.creator = d!["user"] as? String
self.isAllDay = (d!["isAllDay"] as? Bool)!
self.isPrivate = d!["isPrivate"] as! Bool
self.count = (d!["count"] as? String)!
self.date = d?["day"] as? String
self.month = d?["month"] as? String
self.year = d?["year"] as? String
self.bridgesDays = doesEventBridgeDays()
//MARK: re-implement these functions
isInvitee()
}
Run Code Online (Sandbox Code Playgroud)
我刚刚将其从使用字符串切换为时间戳,现在应用程序上的和字段出现unexpectedly found nil错误。startend
断点向我展示了这一点:
正如您所看到的,开始和结束字段现在表示Failed to get the 'some' field from optional start/end(start并且end现在都是Date对象)
我要么不明白我在网上读的内容,要么互联网上没有关于此的问题/博客文章等,所以
这是什么意思?
我该如何解决它?
很高兴回答任何进一步的问题以帮助解决此问题:)
谢谢 :)
额外的信息*
小智 1
这是失败的,因为代码中的事件对象试图将“开始”和“结束”属性存储为日期?但您正在从 Firebase Firestore 中将它们作为时间戳检索。
您需要执行中间步骤来解开值,然后获取日期对象。
if let end = data["end"] as? Timestamp {
self.end = end.dateValue()
}
Run Code Online (Sandbox Code Playgroud)
另外,您确实应该安全地解开 document.data() 以避免使用 d! 并冒着崩溃的风险。
if let data = document.data(){
// extract values
}
Run Code Online (Sandbox Code Playgroud)
最后,可能值得阅读有关使用 Firestore 发送/检索自定义对象的文档。它让一切变得更加容易。
Firestore.firestore().collection("Event").document(id).getDocument { (document, error) in
if error != nil {
print(error?.localizedDescription as Any)
}
guard let document = document else {
// failed to unwrap document
return
}
if document.exists {
let result = Result {
try document.data(as: Event.self)
}
switch result {
case .success(let event):
if let event = event {
// do something with Event object
} else {
// failed to unwrap Event
}
case .failure:
// failed to form Event
}
} else {
// document doesn't exist
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2715 次 |
| 最近记录: |