[英]crash in core data xcode 8
i am facing an issue in IOS swift Xcode 8 我在IOS Swift Xcode 8中遇到问题
after i setup my Core Data and before i insert any junk data in purpose of testing the fetch function the app run correctly with no crash and no data but after i insert a data the app crash with below message 在我设置了核心数据之后,在为了测试获取功能而插入任何垃圾数据之前,该应用正确运行,没有崩溃,也没有数据,但是在我插入数据之后,该应用崩溃并显示以下消息
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UITableViewCell 0x7f9b26054c00> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Deatials.
here is my code 这是我的代码
//
import UIKit
import CoreData
// the NSFetchedResultsControllerDelegate needed to start woring in all the function for datacore
class MainVC: UIViewController , UITableViewDelegate, UITableViewDataSource,NSFetchedResultsControllerDelegate{
// definning the main view the table and the segment.
@IBOutlet weak var TableView: UITableView!
@IBOutlet weak var segment: UISegmentedControl!
// need to difine the NSFetchedResultsController to define the remaining 3 functions for the tableview
var controller : NSFetchedResultsController<Item>!
override func viewDidLoad() {
super.viewDidLoad()
generateTeseData()
attemptFetch()
TableView.dataSource = self
TableView.delegate = self
}
// we need to define 3 main fucntions for the table view
func numberOfSections(in tableView: UITableView) -> Int {
if let sections = controller.sections{
return sections.count
}
return 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// need to difine number of rows in section by NSFetchedResultsController
if let sections = controller.sections{
let sectionInfo = sections[section]
return sectionInfo.numberOfObjects
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// linking the cell var with the class of itemCell been created in the View Folder
let cell = tableView.dequeueReusableCell(withIdentifier: "CellItem", for: indexPath) as! ItemCell
// function been created below:
configureCell(cell: cell, indexPath: indexPath as NSIndexPath)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150
}
func configureCell (cell:ItemCell, indexPath: NSIndexPath){
let item = controller.object(at: indexPath as IndexPath)
cell.ConfigureCellsInCellclass(item: item)
}
func attemptFetch () {
let fetchrequest:NSFetchRequest<Item> = Item.fetchRequest()
let datesort = NSSortDescriptor(key: "created", ascending: false)
fetchrequest.sortDescriptors = [datesort]
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)")
}
}
// these two function for the update in the tableview
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
TableView.beginUpdates()
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
TableView.endUpdates()
}
//this function to preforme all the functions for the <Data Core>
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch(type)
{
case.insert:
if let indexpath = newIndexPath {
TableView.insertRows(at: [indexpath], with: .fade)
}
break
case.delete:
if let indexpath = indexPath{
TableView.deleteRows(at: [indexpath], with: .fade)
}
break
case.update:
if let indexpath = indexPath {
let cell = TableView.cellForRow(at: indexpath) as! ItemCell
configureCell(cell: cell, indexPath: indexPath! as NSIndexPath)
}
break
case.move:
if let indexpath = indexPath {
TableView.deleteRows(at: [indexpath], with: .fade)
}
if let indexpath = indexPath {
TableView.insertRows(at: [indexpath], with: .fade)
}
break
}
}
at this point the system will cun without any crash but after i add the insert function in below it's start crashing 在这一点上,系统将cun没有崩溃,但我在下面添加插入功能后,它开始崩溃
func generateTeseData(){
let item = Item(context: context)
item.title = "IMAC"
item.price = 2000
item.details = "Soon"
}
this is my view cell file 这是我的视图单元文件
class ItemCell: UITableViewCell {
// this view to take all the label from the view and link it here
@IBOutlet weak var thump: UIImageView!
@IBOutlet weak var Title: UILabel!
@IBOutlet weak var Price: UILabel!
@IBOutlet weak var Deatials: UILabel!
// using this function to set the values of the items with the labels been linked before in upper section
func ConfigureCellsInCellclass (item:Item){
Title.text = item.title
Price.text = ("$\(item.price)")
Deatials.text = item.details
}
}
thank you guys in advance 提前谢谢你们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.