查询中有什么问题?
let uid = Auth.auth().currentUser?.uid
let itemId = selectedItem.objectID
var isOn = false
func like(){
if isOn == true {
likeRef.delete(){err in
if let err = err{
print("Error")
}else{
self.likeImageView.image = UIImage(named: "detail_like.png")
self.isOn = false
print("delete Success")
}
}
}else if isOn == false {
let like = Like(uid: uid!, itemId: itemId!, status: true)
db.collection("likes").addDocument(data: like.dictionary()){ err in
if let err = err {
print("Error adding document: \(err)")
} else {
self.isOn = true
self.likeImageView.image = UIImage(named: "like_after.png")
}
}
print(isOn)
}
}
它的值为addDocument
。
值不为零。
我第一次遇到此错误。 是什么原因?
答案 0 :(得分:0)
发生错误,因为let itemId = selectedItem.objectID
没有值。
因此,当我直接设置它而不声明常量时,错误消失了。
if isOn == true {
db.collection("likes").document(likeObjectID).delete(){err in
if let err = err{
print("Error")
}else{
self.likeImageView.image = UIImage(named: "detail_like.png")
self.isOn = false
print("delete Success")
}
}
}else if isOn == false {
let like = Like(uid: uid!, itemId: itemId!, status: true)
db.collection("likes").addDocument(data: like.dictionary()){ err in
if let err = err {
print("Error adding document: \(err)")
} else {
self.isOn = true
self.likeImageView.image = UIImage(named: "like_after.png")
}
}
print(isOn)
}