我有一个应用程序可以保存用户输入,由Core数据实现。在构建我的项目时,我得到一个线程1:信号SIGABRT ,并出现以下错误消息:
2016-03-06 09:34:47.893 On The Go [59689:24714046] ***由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'" On_The_Go .Agenda"不是NSManagedObject的子类。'
这是我的Agenda.swift
课程:
import Foundation
import CoreData
class Agenda {
@NSManaged var subject:String
@NSManaged var deadline:String
@NSManaged var urgent:NSNumber?
}
这是我的MainTableViewController.swift
,它充当我的初始视图控制器,用户输入的数据出现(我只显示涉及此错误的代码以防止过多的代码):
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! Agenda
行,我收到警告:
来自' NSManagedObject'不相关的类型'议程'总是失败
答案 0 :(得分:0)
我自己修好了,改变了
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?
}
答案 1 :(得分:0)
您尚未声明类扩展NSManagedObject的类议程。您的Agenda.swift文件应如下所示:
import Foundation
import CoreData
@objc (Agenda)
class Agenda : NSManagedObject{
@NSManaged var subject:String
@NSManaged var deadline:String
@NSManaged var urgent:NSNumber?
}