Core-data: when accessing a relationship, the count method on NSSet fails

Posted by lordsandwich on Stack Overflow See other posts from Stack Overflow or by lordsandwich
Published on 2010-03-26T20:03:29Z Indexed on 2010/03/26 20:43 UTC
Read the original article Hit count: 419

Filed under:
|
|
|

I'm trying to access a relationship (one to many) programatically. My Data model contains an NSManagedEntity called language (with a two string attributes) with a relationship to an entity called WordCategory (one-to-many). I use an NSFetchRequest to get all the Language entities. that works fine. I get the valueForKey for the relationship and that works fine. I can work with its objects. However, when I try to send the message count to the NSSet that stores the WordCategory objects I get a

In other words, this line works:

       NSLog(@"word category count %@",[[wordCategory anyObject] valueForKey:@"name"]);

This one doesn't:

NSLog(@"word category count %@",[wordCategory count]

I get a the message: EXC_BAD_ACCESS in the debugger.

Here's the rest of the code:

NSManagedObjectContext *moc = [myAppDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Language" inManagedObjectContext:moc]];

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error: &error];
if (error) {
    [NSApp presentError:error];
    return;
}

NSManagedObject *obj = [results objectAtIndex:0];
NSSet *wordCategory = [obj valueForKey:@"category"];
NSLog(@"word category count %@",[wordCategory count]);

I'll appreciate any light than anybody can shed in this mystery. Thanks for your help!

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about objective-c