objective-c - 我的 Swift 应用程序出错但我不知道如何修复它?你能帮助我吗?
问题描述
你好我的应用程序致命错误:在隐式展开可选值错误时意外发现 nil。我了解到选项存在问题。但我不知道如何自己改变它,你能帮我吗?
ref = db.products(category: category.id)
这是给出错误的地方
import UIKit
import FirebaseFirestore
class ProductsVC: UIViewController, ProductCellDelegate {
// Outlets
@IBOutlet weak var tableView: UITableView!
// Variables
var products = [Product]()
var category: Category!
var listener: ListenerRegistration!
var db : Firestore!
var showFavorites = false
override func viewDidLoad() {
super.viewDidLoad()
db = Firestore.firestore()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib(nibName: Identifiers.ProductCell, bundle: nil), forCellReuseIdentifier: Identifiers.ProductCell)
setupQuery()
}
func setupQuery() {
var ref: Query!
if showFavorites {
ref = db.collection("users").document(UserService.user.id).collection("favorites")
} else {
ref = db.products(category: category.id)
}
listener = ref.addSnapshotListener({ (snap, error) in
if let error = error {
debugPrint(error.localizedDescription)
}
snap?.documentChanges.forEach({ (change) in
let data = change.document.data()
let product = Product.init(data: data)
switch change.type {
case .added:
self.onDocumentAdded(change: change, product: product)
case .modified:
self.onDocumentModified(change: change, product: product)
case .removed:
self.onDocumentRemoved(change: change)
}
})
})
}
解决方案
要修复 id 上的展开问题,请执行以下操作:
if let value = ref {
value = db.products(category: category.id)
}
推荐阅读
- amazon-ec2 - EC2 cloudformation模板使用条件添加ebs卷
- python - 如何在 Visual Studio 2017 的 Python 项目中添加现有包(用于 IntelliSense)
- python-3.x - 未使用 ColumnDataSource(Dataframe).data 更新 slider.value 值
- java - 如何在野飞服务器中部署战争?
- laravel - 如何在 Laravel 5.8 中使用 DarkaOnLine / L5-Swagger 记录回调
- python - 在 Python 中查找 3-way ANOVA 的 P 值
- java - 如何读取 Undertow HttpServerExchange 对象中的响应数据?
- c++ - 无法将向量传递给 lambda 函数
- format - 如何使用 esttab 或 estout 从数据中导出
- amazon-web-services - 图像文件上传到 AWS S3 Bucket,但不在 django heroku App 上呈现