【发布时间】:2017-07-18 08:37:23
【问题描述】:
我正在使用核心数据将信息保存到对象,但是当我尝试使用它时,程序崩溃并显示“致命错误:在展开可选值时意外发现 nil”
这是我生成的数据
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
}
【问题讨论】:
-
这意味着有些东西是零。尝试找出哪个属性为 nil,究竟是哪一行导致了崩溃。
-
self.title.text = item.title ,其余部分也是如此
-
但这是否意味着
item为零?item.title是 nil,还是self.title是 nil?这是不同的情况,无论问题是在 CoreData 端还是在您的 Cell 端。 -
我刚刚发现我的插座没有连接!谢谢你指出这一点。
标签: ios swift core-data swift3