【发布时间】:2023-03-11 16:56:02
【问题描述】:
我希望在点击删除按钮时删除 UICollectionViewCell:
@IBAction func deleteButtonClicked() {
error here: delegate?.deleteTrigger(clothes!)
}
衣服:
var clothes: Clothes? {
didSet {
updateUI()
}
}
func deleteTrigger:
func deleteTrigger(clothes: Clothes){
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext!
let en = NSEntityDescription.entityForName("Category", inManagedObjectContext: context)
if let entity = NSEntityDescription.entityForName("Category", inManagedObjectContext: context) {
let indexPath = NSIndexPath()
//var cat : Category = clothing as! Category
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext: NSManagedObjectContext = appDelegate.managedObjectContext!
let fetchRequest = NSFetchRequest(entityName: "Clothes")
let predicate = NSPredicate(format: "category == %@", self.selectedCategory!)
fetchRequest.predicate = predicate
var error: NSError? = nil
var clothesArray = managedContext.executeFetchRequest(fetchRequest, error: &error)!
managedContext.deleteObject(clothesArray[indexPath.row] as! NSManagedObject)
clothesArray.removeAtIndex(indexPath.row)
self.collectionView?.deleteItemsAtIndexPaths([indexPath])
if (!managedContext.save(&error)) {
abort()
}
}
衣服是 Core Data 中的一个实体。有谁知道我为什么会收到这个错误?我正在尝试以一对多的关系从核心数据中删除一个 collectionViewCell。 Category 是父实体, Clothes 是 Category 中的实体。
【问题讨论】:
-
你说的是
delegate?.deleteTrigger(clothes!)- 任何时候你使用感叹号,你都在乞求崩溃。这就是 Swift 中感叹号 的意思。你确实崩溃了。没什么大惊喜。如果您不想这样做,请不要这样做。
标签: ios swift core-data uicollectionview