我正在尝试使用查询删除对象。但是我收到一个向下转换错误,说[PFObject]的向下转换仅解包了您要使用的可选选项!
var query: PFQuery = PFQuery(className: "Posts")
query.whereKey("deleteID", containsString: selectedObjId)
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
for object in (objects as! [PFObject]) {
if error == nil {
//Hide activity indicator and go back to collection view when done deleting
object.delete() // 削除
self.hideActivityIndicator(self.view)
self.quitButtonPressed()
}
}
}
最佳答案
似乎返回的objects
是[PFObject]?
类型的,所以不用向下转换,只需解开对象即可
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
for object in objects! {
do {
//Hide activity indicator and go back to collection view when done deleting
try object.delete() // 削除
} catch let error as NSError {
print(error)
}
}
self.hideActivityIndicator(self.view)
self.quitButtonPressed()
} else {
print(error!)
}
}
您还应该在重复循环之前检查错误是否为零,并且应该在重复循环之后执行一次隐藏指示器并按下按钮的行。
关于swift - 查询中的向下转换错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33756934/