2012-04-12 91 views
0

我想知道是否有可能初始化NSManagedObject的子類? 我有一個類「Actualite」,它是NSManagedObject的子類,當我想初始化這個類時,我得到這個錯誤: 「CoreData:error:無法調用NSManagedObject類Actualite」上的指定初始值設定項,並且應用程序崩潰後此消息 「 - [Actualite setTitre:]:無法識別的選擇發送到實例0x8433be0」初始NSManagedObject子類

這裏是我的代碼:

-(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]; 
} 

,並設置Actualite類的所有屬性initWithDictionary方法。

我也試過

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]; 

的錯誤消失,但應用程序在這一點上停止。我不知道我能做什麼...

有人已經有這個問題嗎?

非常感謝!

回答

4

的想法是,你要求一個新的對象上下文中被創建,然後你使用它:

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