I have a core data model with four entities. The entity Player
has a to-Many relationship to the other entities (Player_Scores,Custom_Exercise,Selected_Exercise
).
In my app delegate, I make NSManagedObjectContext,NSManagedObjectModel,NSPersistentStoreCoordinator
properties in the standard way. Then, in a different view controller, I declare ivars for an NSManagedObjectContext
object and a newPlayer
Player entity in the @interface:
@interface NewProfileViewController()
{
NSManagedObjectContext *context;
Player *newPlayer;
}
Then in an action I have the following code to create a Player entity and enter in its attributes, as well as a Player_Scores
entity and a Selected_Exercise
entity. The code i use successfully adds the attributes for the Player_Scores
and the Player entities. However when I try to add 16 Selected_Exercise
entities in a loop and set their attributes I get a big fat "Property cannot be found on forward class object?" error. HELP!!!!! Like i said the code is the same for Selected_Exercise
and for Player_Scores
. I already tried re-starting, deleting database, etc. It's a compiler error that pops up when i try to do newEx.exercise=@"blahblahblah";
or newEx.suit=@"blahblahblah";
UGH
Below is my code for that method:
//1. save a person to database
newPlayer=[NSEntityDescription
insertNewObjectForEntityForName:@"Player"
inManagedObjectContext:context];
newPlayer.name=newentry;
//NSError *error; [context save:&error];
//2. begin making a score card:
Player_Scores *newScoreCard = [NSEntityDescription
insertNewObjectForEntityForName:@"Player_Scores"
inManagedObjectContext:context];
newScoreCard.date_of_game = [NSDate date];
newScoreCard.player=newPlayer; //attach this score card to the new playe
[newPlayer addScoresObject:newScoreCard];//add the score card to the newplayer
//3. make selected_exercise
NSString *plistCatPath = [[NSBundle mainBundle] pathForResource:@"ListOfExercises" ofType:@"plist"]; //grab plist
NSMutableArray* theDictArray= [[NSMutableArray arrayWithContentsOfFile:plistCatPath] copy];
for(int cnt=0;cnt<[theDictArray count];cnt++){
Selected_Exercise *newEx= [NSEntityDescription
insertNewObjectForEntityForName:@"Selected_Exercise"
inManagedObjectContext:context];
newEx.exercise=[[theDictArray objectAtIndex:cnt]valueForKey:@"exercise"];
newEx.suit=[[theDictArray objectAtIndex:cnt]valueForKey:@"suit"];
[newPlayer addSelected_exerciseObject:newEx];
NSLog(@"added exercise %@ for suit %@ at array index %d",[[theDictArray objectAtIndex:cnt]valueForKey:@"exercise"],[[theDictArray objectAtIndex:cnt]valueForKey:@"suit"],cnt);
}
// Save everything
NSError *error = nil;
if ([context save:&error]) {
NSLog(@"The save was successful!");
} else {
NSLog(@"The save wasn't successful: %@", [error userInfo]);
}