gpt4 book ai didi

ios - 核心数据 xcode 8 崩溃

转载 作者:行者123 更新时间:2023-11-30 12:31:59 25 4
gpt4 key购买 nike

我在 IOS swift 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"
}

这是我的 View 单元文件

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

先谢谢大家了

最佳答案

正如@Larme所说,你的问题与IBOutlet有关,它不再反射(reflect)在类里面。

断开坏 socket

enter image description here

关于ios - 核心数据 xcode 8 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43411997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com