gpt4 book ai didi

ios - fatal error : unexpectedly found nil while unwrapping an Optional value inside an entity

转载 作者:行者123 更新时间:2023-11-29 01:08:23 24 4
gpt4 key购买 nike

我希望在点击删除按钮时删除一个 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 中的实体。

最佳答案

你已经声明了一个属性衣服:

var clothes: Clothes?

您从未给它任何,所以它是nil。因此,当您通过说 clothes! 强制打开它时,您会崩溃。

关于ios - fatal error : unexpectedly found nil while unwrapping an Optional value inside an entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36074348/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com