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
).
我有一个包含四个实体的核心数据模型。实体播放器与其他实体有很多关系(player_score,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:
在我的app委托中,我用标准的方式创建NSManagedObjectContext NSManagedObjectModel NSPersistentStoreCoordinator属性。然后,在另一个视图控制器中,我在@interface中为NSManagedObjectContext对象和newPlayer播放器实体声明ivars:
@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
然后在操作中,我有以下代码来创建一个Player实体并输入其属性,以及player_score实体和Selected_Exercise实体。我使用的代码成功地添加了player_score和Player实体的属性。但是,当我尝试在循环中添加16个Selected_Exercise实体并设置它们的属性时,我得到一个大的fat“属性在forward类对象上找不到”错误。救命! ! ! ! !如我所说,Selected_Exercise和player_score的代码是相同的。我已经尝试重新启动、删除数据库等。当我尝试执行newEx.exercise=@“blahblahblahblah”时,会弹出一个编译错误;或newEx.suit = @“blahblahblah”;啊
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]);
}
1 个解决方案
#1
5
That sounds as if you imported "Player.h", but not "Selected_Exercise.h" in that file.
听起来好像你导入了“播放器”。h”,但不是“Selected_Exercise。h”文件。
"Player.h" probably contains the forward declaration @class Selected_Exercise
, so that the compiler does not complain on
”的球员。h“可能包含转发声明@class Selected_Exercise,这样编译器就不会抱怨了
Selected_Exercise *newEx = [NSEntityDescription ...
But if "Selected_Exercise.h" is not imported, the properties of that class, such as newEx.exercise
, are unknown to the compiler.
但如果“Selected_Exercise。不导入h",该类的属性,如newEx。练习,编译器不知道。
#1
5
That sounds as if you imported "Player.h", but not "Selected_Exercise.h" in that file.
听起来好像你导入了“播放器”。h”,但不是“Selected_Exercise。h”文件。
"Player.h" probably contains the forward declaration @class Selected_Exercise
, so that the compiler does not complain on
”的球员。h“可能包含转发声明@class Selected_Exercise,这样编译器就不会抱怨了
Selected_Exercise *newEx = [NSEntityDescription ...
But if "Selected_Exercise.h" is not imported, the properties of that class, such as newEx.exercise
, are unknown to the compiler.
但如果“Selected_Exercise。不导入h",该类的属性,如newEx。练习,编译器不知道。