崩溃的核心数据xcode 8

时间:2017-04-14 12:57:08

标签: ios swift tableview

我在IOS swift Xcode 8中面临一个问题

在我设置我的核心数据之后,在我插入任何垃圾数据以测试获取功能之前,应用程序运行正常,没有崩溃,没有数据,但在我插入数据后应用程序崩溃,并显示以下消息

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/itemName"
        tools:text="Item Name"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/itemQuantity"
        tools:text="Quantity"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />

</LinearLayout>

这是我的代码

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

提前谢谢你们

1 个答案:

答案 0 :(得分:0)

正如@Larme所说,你的问题与IBOutlet有关,而这个问题不再在课堂上反映出来了。

断开坏插座

enter image description here