我正在使用核心數據將信息保存到對象,但是當我嘗試使用它時,程序崩潰並說「致命錯誤:在解包可選值時意外發現零」我使用coreData保存數據,但對象保持無
這就是我產生
func generateTestData()
{
let item = Item(context: context)
item.title = "New Iphone 7s"
item.price = 2000
item.details = "I wish it's something that is worth to apple , unline Iphone 7"
let item2 = Item(context: context)
item2.title = "Beach House in France"
item2.price = 3000000
item2.details = "I will live there for the rest of my Life , untill then i don't have a life"
}
這是取功能
func attemptFetch()
{
let fetchRequest :NSFetchRequest<Item> = Item.fetchRequest()
let datasort = NSSortDescriptor(key: "created", ascending: false)
fetchRequest.sortDescriptors = [datasort]
let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
self.controller = controller
do{
try controller.performFetch()
}
catch{
let error = error as NSError
print(error.debugDescription)
}
}
墜機發生在這裏,當我嘗試更新我的視圖中的數據
func configureCell(cell : objectItemCell , indexPath :IndexPath)
{
let item = controller.object(at:indexPath)
cell.configureCell(item: item)
}
UITableViewCell類,
func configureCell(item : Item)
{
self.title.text = item.title
self.price.text = "$\(item.price)"
self.details.text = item.details
}
這意味着什麼是零。試着找出哪個屬性是零,哪條線正是導致崩潰。 – Larme
那個說self.title.text = item.title,其餘的同樣去下 –
但是這是否意味着'item'是零?那個'item.title'是零,或者'self.title'是零?這是不同的情況,無論這個問題是在CoreData端還是在Cell端。 – Larme