Init NSManagedObject子类

时间:2012-04-12 12:30:30

标签: core-data nsmanagedobject init

我想知道是否可以初始化NSManagedObject的子类? 我有一个类“Actualite”,它是NSManagedObject的子类,当我想初始化这个类时,我收到此错误: “CoreData:错误:无法在NSManagedObject类Actualite上调用指定的初始化程序”,并且此消息“ - [Actualite setTitre:]:无法识别的选择器发送到实例0x8433be0”后应用程序崩溃“

这是我的代码:

-(void) recupererActualites {
listeNouvellesActualites = [[NSMutableArray alloc] init];

// Convert the supplied URL string into a usable URL object
NSURL *url = [NSURL URLWithString:FACE06_RSS];

// Create a new rssParser object based on the TouchXML "CXMLDocument" class, this is the
// object that actually grabs and processes the RSS data
CXMLDocument *rssParser = [[CXMLDocument alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding options:0 error:nil];

// Create a new Array object to be used with the looping of the results from the rssParser
NSArray *resultNodes = NULL;

// Set the resultNodes Array to contain an object for every instance of an  node in our RSS feed
resultNodes = [rssParser nodesForXPath:@"//item" error:nil];

NSMutableArray* tmp = [[NSMutableArray alloc] init];
for (CXMLElement* resultElement in resultNodes) {
    // Create a temporary MutableDictionary to store the items fields in, which will eventually end up in blogEntries
    NSMutableDictionary *blogItem = [[NSMutableDictionary alloc] init];
    NSMutableArray* categories = [[NSMutableArray alloc] init];

    // Create a counter variable as type "int"
    int counter;

    // Loop through the children of the current  node
    for(counter = 0; counter < [resultElement childCount]; counter++) {
        // Add each field to the blogItem Dictionary with the node name as key and node value as the value
        if([[[resultElement childAtIndex:counter] name] isEqual:@"category"])
            [categories addObject:[[resultElement childAtIndex:counter] stringValue]];
        else {
            if ([[resultElement childAtIndex:counter] stringValue] != nil)
                [blogItem setObject:[[resultElement childAtIndex:counter] stringValue] forKey:[[resultElement childAtIndex:counter] name]];
            else
                [blogItem setObject:@"" forKey:[[resultElement childAtIndex:counter] name]];
        }
    }

    Actualite* actu = [[Actualite alloc] init];
    [blogItem setObject:categories forKey:@"categories"];
    [actu initWithDictionnary:blogItem];
    [tmp addObject:actu];
    //[actu release];

    [categories release];
    [blogItem release];
}
listeNouvellesActualites = tmp;

[rssParser release];
resultNodes = nil;

// Stockage des actualités en local
[self stockerActualites];
}

initWithDictionary方法设置Actualite类的所有属性。

我也试过

Actualite* actu = [[Actualite alloc] initWithEntity:[NSEntityDescription entityForName:@"Actualite" inManagedObjectContext:managedObjectContext] insertIntoManagedObjectContext:managedObjectContext]; 

Actualite* actu = (Actualite*)[NSEntityDescription entityForName:@"Actualite" inManagedObjectContext:managedObjectContext]; 

而不是

Actualite* actu = [[Actualite alloc] init]; 

错误消失,但应用程序此时停止。我不知道我该怎么做......

有人已经有这个问题吗?

非常感谢!

1 个答案:

答案 0 :(得分:4)

这个想法是你要求在一个上下文中创建一个新对象然后你使用它:

Actualite* actu = [NSEntityDescription insertNewObjectForEntityForName:@"Actualite" inManagedObjectContext:managedObjectContext];