CoreData insertNewObjectForEntityForName 导致崩溃

CoreData insertNewObjectForEntityForName Causing Crash

我一直在追逐一个错误,但我无法弄清楚。我有一个 class 接受一堆解析数据,然后调用一个方法来为从解析数据创建的每个元素创建新的核心数据 "Article" 对象。我在下面展示了如何声明 NSManagedObjectContext。

你也会看到方法

Article.createFLOArticleWithStructure(element, inManagedObjectContext: self.articleContext)

我将此方法放在扩展中以清理代码。该方法如下所示。

import Foundation

class FLODataHandler : NSObject, FLOConnectionHandlerDelegate, FLOParserHandlerDelegate, TriathleteParserHandlerDelegate, VeloNewsParserHandlerDelegate, CyclingNewsParserHandlerDelegate, RoadBikeActionParserHandlerDelegate, IronmanParserHandlerDelegate
{
    let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    lazy var articleContext: NSManagedObjectContext = self.appDelegate.managedObjectContext!

    func floParserHandlerDidFinishParsing(parserHandler : FLOParserHandler)
    {
        for element in self.floParserHandler!.XMLParsedDataArray!
        {
            // The pubDate, tilte and link and indicator have been added to the titleLinkArray.  I will now add the data to a Core Data Entity
            // in the Article+NewsFeedArticle class.
            Article.createFLOArticleWithStructure(element, inManagedObjectContext: self.articleContext)
        }
    }

分机号

extension Article
{
    class func createFLOArticleWithStructure(articleStructure: DateTitleLink, inManagedObjectContext context: NSManagedObjectContext) -> (Article)
    {
        // Core data is much simpler in Swift.  I have not commented this code since I do not know if it's working yet.

        var article = Article()

        //var entity = NSEntityDescription("Article", inManagedObjectContext: context)
        let fetchRequest = NSFetchRequest(entityName: "Article")
        let predicate = NSPredicate(format: "feed == 'FLO Cycling' AND title == %@", articleStructure.title!)
        let sortDescriptor = NSSortDescriptor(key: "pubDate", ascending: true)
        fetchRequest.sortDescriptors = [sortDescriptor]

        // Set up NSError
        var fetchError : NSError?
        // When you perform a fetch the returned object is an array of the Atricle Entities.
        let fetchedObjects = context.executeFetchRequest(fetchRequest, error: &fetchError) as! [Article]

        if fetchedObjects.count > 1
        {
            println("Error! in Article+NewFeedArticle.swift")
        }
        else if fetchedObjects.count == 0
        {
            article = NSEntityDescription.insertNewObjectForEntityForName("Article", inManagedObjectContext: context) as! Article
            article.feed = "FLO Cycling"
            article.pubDate = articleStructure.date!
            article.title = articleStructure.title!
            article.link = articleStructure.link!
            article.theNewArticle = NSNumber(int: 1)

            var error : NSError?
            if(context.save(&error))
            {
                println(error!.localizedDescription)
            }
        }
        else if fetchedObjects.count == 1
        {
            article = fetchedObjects[fetchedObjects.endIndex - 1]
        }

        return article
    }

当我 运行 代码时,我在扩展代码的以下行停止并收到以下错误。

article = NSEntityDescription.insertNewObjectForEntityForName("Article", inManagedObjectContext: context) as! Article

CoreData: error: Failed to call designated initializer on NSManagedObject class 'FLOCycling1_1_1.Article' CoreData: warning: Unable to load class named 'Article' for entity 'Article'. Class not found, using default NSManagedObject instead. Could not cast value of type 'NSManagedObject_Article_' (0x7fe59c3515e0) to 'FLOCycling1_1_1.Article' (0x106139f70).

我已在线阅读有关在数据模型中使用前缀的信息。我已经添加了这个无济于事。如果您知道如何解决此错误,我将不胜感激。

已添加****

这是应要求提供的 Article.swift 文件。

import Foundation
import CoreData

@objc class Article: NSManagedObject
{
    @NSManaged var feed: String
    @NSManaged var link: String
    @NSManaged var pubDate: NSDate
    @NSManaged var theNewArticle: NSNumber
    @NSManaged var title: String
}

保重,

乔恩

这个问题与我在此处这一行中的文章声明有关。

var article = Article()

在 Swift 版本中调用上面的代码行为 Article 分配和初始化内存。在我的代码的 Objective-C 版本中,我调用了以下代码。

Article *article = nil;

虽然我不确定为什么,但分配和初始化文章是问题所在。我在这里 post 发现了一个类似的错误。

Failed to call designated initializer on NSManagedObject class 'ClassName'

为了解决这个问题,我将代码更改为

var article = Article?()

为了清楚起见,我还将文章 class 更改为 ProjectName.Article。

我希望这对其他人有帮助。

保重,

乔恩