Reputation: 231
I have a project which uses Core Data. There is an entity called CDGroup which has a one to many relationship with an entity called CDChapter, the relationship is called chapters. They both have corresponding classes (which are subclasses of NSManagedObject.)
When I retrieve a CDGroup object its class is CDGroup which is correct, the issue however is that if I then attempt to access the chapters set of that group like this:
NSLog(@"%@",[group.chapters objectAtIndex:0].name);
I get an error because the chapter that is retrieved is of type NSManagedObject not CDChapter. I've tried casting the chapter to the correct class but I still get the same issue.
How do I make this work?
Thanks,
Joe
Upvotes: 0
Views: 80
Reputation: 894
chapters refers to an NSSet of objects that belong to the relationship. Try using:
((CDChapter*)[[group.chapters allObjects] objectAtIndex:0]).name;
Upvotes: 0
Reputation: 119242
You can't use dot notation with cast objects. Either split the call out into two lines:
CDChapter *chapter = [[group.chapters allObjects] objectAtIndex:0];
NSLog(@"%@",chapter.name);
Or use the accessor method rather than dot notation:
NSLog(@"%@",[(CDChapter*)[[group.chapters allObjects] objectAtIndex:0] name]);
Upvotes: 2