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
}
}
感謝你們提前
你的問題是不相關的CoreData,這是關於你的XIB /故事板。你在IBOutlet與UILabel''Deatials'有一個不好的聯繫。看看你最喜歡的搜索引擎'終止應用程序由於未捕獲的異常'NSUnknownKeyException',原因:'[XXX setValue:forUndefinedKey:]:這個類不是密鑰值編碼兼容的密鑰XXX'在那裏你刪除XXX是特定的對您的問題,但解決方案是可重複的。 – Larme
也正確的拼寫是'詳細信息' – Alistra
@Larme謝謝你的回覆我只是刪除了IBOutlet之間的所有連接,並從開始創建它,但仍然崩潰, 我連接IBOulet與視圖類從單元格視圖是否有任何特殊方式做到這一點 – OWA