CoreData insertNewObjectForEntityForName导致崩溃

时间:2015-05-26 02:34:41

标签: core-data nsmanagedobject

我现在已经追了一段时间了,我无法弄明白。我有一个类,它接受一堆解析数据,然后调用一个方法来创建新的核心数据"文章"从解析数据创建的每个元素的对象。我已经展示了如何在下面声明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:错误:无法调用指定的初始化程序   NSManagedObject类' FLOCycling1_1_1。文章' CoreData:警告:   无法加载名为' Article'对于实体' Article'。不是   发现,使用默认的NSManagedObject。无法施放价值   类型' NSManagedObject_Article _' (0x7fe59c3515e0)来   ' FLOCycling1_1_1.Article' (0x106139f70)。

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

ADDED ****

这是应要求提供的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
}

小心,

乔恩

1 个答案:

答案 0 :(得分:0)

这个问题与我在这一行中宣布的条款有关。

Article *article = nil;

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

var article = Article?()

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

Failed to call designated initializer on NSManagedObject class 'ClassName'

要解决此问题,我将代码更改为

-b

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

我希望这有助于其他人。

小心,

乔恩