ios - 核心数据终止错误

标签 ios swift class core-data

我有一个保存用户输入的应用程序,由核心数据实现。构建项目时,我收到线程 1:信号 SIGABRT 和以下错误消息:

2016-03-06 09:34:47.893 On The Go[59689:24714046] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '"On_The_Go.Agenda" is not a subclass of NSManagedObject.'

这是我的 Agenda.swift 类:

import Foundation
import CoreData

class Agenda {

@NSManaged var subject:String
@NSManaged var deadline:String
@NSManaged var urgent:NSNumber?
}

这是我的 MainTableViewController.swift,它充当我的初始 View Controller ,用户输入的数据出现在其中(我只显示了涉及此错误的代码,以防止过多的代码):

import UIKit
import CoreData

class MainTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {

var content:[Agenda] = []
var fetchResultController:NSFetchedResultsController!

override func viewDidLoad() {
    super.viewDidLoad()

    let fetchRequest = NSFetchRequest(entityName: "Agenda")
    let sortDescriptor = NSSortDescriptor(key: "subject", ascending: true)
    fetchRequest.sortDescriptors = [sortDescriptor]

    if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {

        fetchResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
        fetchResultController.delegate = self

        do {
            try fetchResultController.performFetch()
            content = fetchResultController.fetchedObjects as! [Agenda]
        } catch {
            print(error)
        }
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func controllerWillChangeContent(controller: NSFetchedResultsController) {
    tableView.beginUpdates()
}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {

    switch type {
        case .Insert:
            if let _newIndexPath = newIndexPath {
                tableView.insertRowsAtIndexPaths([_newIndexPath], withRowAnimation: .Fade)
            }

        case .Delete:
            if let _indexPath = indexPath {
                tableView.deleteRowsAtIndexPaths([_indexPath], withRowAnimation: .Fade)
            }

        case .Update:
            if let _indexPath = indexPath {
                tableView.reloadRowsAtIndexPaths([_indexPath], withRowAnimation: .Fade)
            }

        default:
            tableView.reloadData()
    }

    content = controller.fetchedObjects as! [Agenda]
}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
    tableView.endUpdates()
}

}

这是 AddItemController.swift,用户在其中添加他们的信息:

import UIKit
import CoreData

class AddItemController: UITableViewController {

@IBOutlet var subjectTextField:UITextField!
@IBOutlet var deadlineTextField:UITextField!
@IBOutlet var yesButton:UIButton!
@IBOutlet var noButton:UIButton!

var urgent = true
var content:Agenda!

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func save(sender:UIBarButtonItem) {

    let subject = subjectTextField.text
    let deadline = deadlineTextField.text

    if subject == "" || deadline == "" {
        let alertController = UIAlertController(title: "Oops!", message: "Not all of the fields have been filled in. Please fill them in before proceeding.", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)

        return
    }

    if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {

        content = NSEntityDescription.insertNewObjectForEntityForName("Agenda", inManagedObjectContext: managedObjectContext) as! Agenda
        content.subject = subject!
        content.deadline = deadline!
        content.urgent = urgent

        do {
            try managedObjectContext.save()
        } catch {
            print(error)
            return
        }
    }

    dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func toggleUrgentButton(sender: UIButton) {

    if sender == yesButton {
        urgent = true
        yesButton.backgroundColor = UIColor(red: 170.0/255.0, green: 240.0/255.0, blue: 255.0/255.0, alpha: 1.0)
        noButton.backgroundColor = UIColor.lightGrayColor()
    } else if sender == noButton {
        urgent = false
        yesButton.backgroundColor = UIColor.lightGrayColor()
        noButton.backgroundColor = UIColor(red: 170.0/255.0, green: 240.0/255.0, blue: 255.0/255.0, alpha: 1.0)
    }
}
}

仅供引用,行content = NSEntityDescription.insertNewObjectForEntityForName("Agenda", inManagedObjectContext: ManagedObjectContext) as!议程,我收到警告:

Cast from 'NSManagedObject' to unrelated type 'Agenda' always fails

最佳答案

我自己修复了它,我改变了

import Foundation
import CoreData

class Agenda {

@NSManaged var subject:String
@NSManaged var deadline:String
@NSManaged var urgent:NSNumber?
}

import Foundation
import CoreData

class Agenda:NSManagedObject {

@NSManaged var subject:String
@NSManaged var deadline:String
@NSManaged var urgent:NSNumber?
}

关于ios - 核心数据终止错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35828249/

相关文章:

ios - 如何将 JSONDecoder/Codable 与此日期格式一起使用?

ios - UISwitch 关闭/打开 - 图像不会出现

ios - 从入门/教程屏幕执行 segue

swift - 可以使用 forEach 迭代数组,但不能使用 List

javascript - 如何将 NSArray 传递给 JavaScript 文件并在 JavaScript 中打印该数组?

iphone - 即使正确调用了 NSNotification 处理程序方法,也不会在该方法内执行任何代码

ios - 当我尝试登录注册用户并通过解析启用推送通知时,我的应用程序崩溃了

Eclipse CDT - 在派生类中显示继承的成员?

javascript - append 元素上的绑定(bind)方法

java - 多个类(class)的绘画