我的模型有两个实体Artist
和Album
,并且Album
有一个Artist
实例成员。我使用以下代码预先填充了我的模型,但只找到了最后一个 Album ,即ablum3
与Artist Beatles
. 对于album1
, album2
,artist
归档是nil
.
一定有什么我没有发现的错误...
//create an artist
NSManagedObject *artist = [NSEntityDescription
insertNewObjectForEntityForName:@"Artist"
inManagedObjectContext:__managedObjectContext];
[artist setValue:@"Beatles" forKey:@"name"];
//populate the data
NSArray *albums = [NSArray arrayWithObjects:@"album1",@"album2",@"album3", nil];
for (NSString *title in albums){
//populate the data
NSManagedObject *album = [NSEntityDescription
insertNewObjectForEntityForName:@"Album"
inManagedObjectContext:__managedObjectContext];
[album setValue:title forKey:@"title"];
[album setValue:artist forKey:@"artist"];
}