2017-04-14 252 views
0

我在IOS迅速的Xcode 8崩溃的核心数据的Xcode 8

面临的问题后,我安装我的核心数据和我在测试的目的,将任何垃圾数据之前取功能的应用程序没有崩溃正确运行没有数据,但之后我插入数据下面的消息应用程序崩溃

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UITableViewCell 0x7f9b26054c00> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Deatials. 

这里是我的代码

// 

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 
} 
} 

此时系统将不存任何崩溃 但之后,我在它下面添加插入功能的开始崩溃

func generateTeseData(){ 
    let item = Item(context: context) 

    item.title = "IMAC" 
    item.price = 2000 
    item.details = "Soon" 
} 

这是我的观察室文件

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 
} 
} 

感谢你们提前

+1

你的问题是不相关的CoreData,这是关于你的XIB /故事板。你在IBOutlet与UILabel''Deatials'有一个不好的联系。看看你最喜欢的搜索引擎'终止应用程序由于未捕获的异常'NSUnknownKeyException',原因:'[XXX setValue:forUndefinedKey:]:这个类不是密钥值编码兼容的密钥XXX'在那里你删除XXX是特定的对您的问题,但解决方案是可重复的。 – Larme

+0

也正确的拼写是'详细信息' – Alistra

+0

@Larme谢谢你的回复我只是删除了IBOutlet之间的所有连接,并从开始创建它,但仍然崩溃, 我连接IBOulet与视图类从单元格视图是否有任何特殊方式做到这一点 – OWA

回答

0

由于@Larme说,你的问题是相关的到IBOutlet,这不再反映在课堂上。

断开坏插座

enter image description here