我的 Swift 应用出现错误,但我不知道如何修复它?你能帮助我吗?
My Swift App gave an Error but I don't know how to Fix it? Can you help me?
你好我的应用程序致命错误:在隐式展开可选值错误时意外发现 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)
}
你好我的应用程序致命错误:在隐式展开可选值错误时意外发现 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)
}