我已经建立了一个包含几个字符串字段和几个数组字段的模型。该模型保存为:
- (void)saveLevel:(NSString*)level traps:(NSArray*)traps whirls:(NSArray*)whirls accels:(NSArray*)accels walls:(NSArray*)walls dest:(NSString*)dest jupiter:(NSString*)jupiter rating:(NSNumber*)pRating;
{
if (m_pMOC == nil)
// Retrieves the managed object context
NSManagedObject* pNewLevel = [NSEntityDescription insertNewObjectForEntityForName:@"Level" inManagedObjectContext:m_pMOC];
NSDate* pDate = [NSDate date];
// Must have these four attributes
[pNewLevel setValue:level forKey:@"Level_ID"];
[pNewLevel setValue:jupiter forKey:@"Ball"];
[pNewLevel setValue:pDate forKey:@"Creation_Date"];
[pNewLevel setValue:dest forKey:@"Destination"];
[pNewLevel setValue:pRating forKey:@"Rating"];
// Optional attributes
if ([traps count] != 0)
[pNewLevel setValue:traps forKey:@"Traps"];
if ([whirls count] != 0)
[pNewLevel setValue:whirls forKey:@"Whirls"];
if ([accels count] != 0)
[pNewLevel setValue:accels forKey:@"Accelerators"];
if ([walls count] != 0)
[pNewLevel setValue:walls forKey:@"Walls"];
NSError* pError;
if (![m_pMOC save: &pError])
// etc...
}
我有一个 DataManager 类,它处理从/到 Core Data 的获取/保存,在 Manager 内部,我验证了当我获取实体时,数组被获取,但是当它返回到调用它的类时,数组为零(另一方面,字符串可以很好地到达)。下面是 fetch 代码和从 fetch 中解析出返回值的代码:
- (NSArray*) getLevelWithID:(NSString*)level
{
NSEntityDescription *pEntityDescription = [NSEntityDescription entityForName:@"Level" inManagedObjectContext:m_pMOC];
NSFetchRequest *pRequest = [[[NSFetchRequest alloc] init] autorelease];
[pRequest setEntity:pEntityDescription];
NSPredicate* pPredicate = [NSPredicate predicateWithFormat:@"Level_ID == %@", level];
[pRequest setPredicate: pPredicate];
NSError* pError;
NSArray* pLevels = [m_pMOC executeFetchRequest:pRequest error:&pError];
if (pLevels == nil)
{
NSLog(@"Could not find the level with Level_ID = %@",level);
abort();
}
NSArray* pAccels = [pLevels valueForKey:@"Accelerators"];
NSArray* pTraps = [pLevels valueForKey:@"Traps"];
NSArray* pWalls = [pLevels valueForKey:@"Walls"];
NSArray* pWhirls = [pLevels valueForKey:@"Whirls"];
return pLevels;
}
我在最后四个数组上设置了一个断点,它们中有对象,但在检索它们的函数中(如下所示),它们是 nil。
- (void) initLevel:(NSArray*)pLevelObjects
{
Level* pLevel = [pLevelObjects objectAtIndex:0];
NSString* pJupiter = pLevel.Ball;
NSString* pDest = pLevel.Destination;
NSArray* pAccels = [pLevel valueForKey:@"Accelerators"];
NSArray* pTraps = [pLevel valueForKey:@"Traps"];
NSArray* pWalls = [pLevel valueForKey:@"Walls"];
NSArray* pWhirls = [pLevel valueForKey:@"Whirls"];
... (other initialization of the level) ...
}
我对此感到困惑。字符串值存在,但数组不存在。我最初尝试使用点表示法(NSArray* pAccels = pLevel.Accelerators 等),但结果相同。
想法?
编辑:从视图控制器调用 initLevel:
- (void) startGameWithLevelID:(NSString*)pLevelID
{
NSLog(@"MyViewController - beginGameWithLevelID");
NSArray* pLevel = [[DataManager getDataManager] getLevelWithID:pLevelID];
if (pLevel == nil || [pLevel count] == 0)
{
NSLog(@"Didn't retrieve any level with id %@", pLevelID);
abort();
}
else
{
CGRect newFrame = makeScreen([UIScreen mainScreen].applicationFrame);
GameBoard* pGB = [[GameBoard alloc] initWithFrame: newFrame];
pGB.m_pMyVC = self;
[pGB initLevel: pLevel];
[self.view addSubview: (UIView*)pGB];
[pGB release];
}
}
编辑:我已经将数组重新分解为与关卡一对一关系的单独实体;该级别与这些实体具有一对多的关系。我还更改了名称以更好地遵守约定:所有属性和关系现在都以小写字母开头。我分别用 a_ 和 r_ 作为属性和关系的前缀。保存“-[NSCFString _isKindOfEntity:]:无法识别的选择器发送到实例 0x4d83120”时出现错误。这不是很有帮助,但我会在发现问题时通知您。目前,保存代码如下所示:
- (void)saveLevel:(NSString*)level traps:(NSArray*)traps whirls:(NSArray*)whirls accels:(NSArray*)accels walls:(NSArray*)walls dest:(NSString*)dest jupiter:(NSString*)jupiter rating:(NSNumber*)pRating;
{
if (m_pMOC == nil)
{ // Code to get the managed object context from the delegate
}
NSManagedObject* pNewLevel = [NSEntityDescription insertNewObjectForEntityForName:@"Level" inManagedObjectContext:m_pMOC];
NSManagedObject* pNewBall = [NSEntityDescription insertNewObjectForEntityForName:@"Ball" inManagedObjectContext:m_pMOC];
[pNewBall setValue:jupiter forKey:@"a_Bounds"];
NSManagedObject* pNewDest = [NSEntityDescription insertNewObjectForEntityForName:@"Dest" inManagedObjectContext:m_pMOC];
[pNewDest setValue:dest forKey:@"a_Bounds"];
NSDate* pDate = [NSDate date];
// Must have these four attributes
[pNewLevel setValue:level forKey:@"a_Level_ID"];
[pNewLevel setValue:pDate forKey:@"a_Creation_Date"];
[pNewLevel setValue:pRating forKey:@"a_Rating"];
[pNewLevel setValue:@"Bob Dole" forKey:@"a_CreatedBy"];
[pNewLevel setValue:pNewBall forKey:@"r_Ball"];
[pNewLevel setValue:pNewDest forKey:@"r_Dest"];
// Optional attributes
if ([traps count] != 0)
[[pNewLevel mutableSetValueForKey: @"r_Trap"] addObjectsFromArray: traps];
if ([whirls count] != 0)
[[pNewLevel mutableSetValueForKey: @"r_Whirl"] addObjectsFromArray: whirls];
if ([accels count] != 0)
[[pNewLevel mutableSetValueForKey: @"r_Accel"] addObjectsFromArray: accels];
if ([walls count] != 0)
[[pNewLevel mutableSetValueForKey: @"r_Wall"] addObjectsFromArray: walls];
NSError* pError;
if (![m_pMOC save: &pError])
{ // Error saving
}
else
{ // Successfully saved
}
}
再次感谢您的帮助,对于冗长的帖子感到抱歉,但我希望这将有助于其他 Core Data 新手(如我)学习一些东西。
最终编辑:好的,所以在重构之后,我终于找到了将数组保存为对象的解决方案(尽管我不确定这是否是最有效的方法)。下面只是添加一堆“陷阱”的代码。传递给函数的数组是 Trap 的边界,将被存储。然后与关卡形成关系(注意它不必同时由陷阱和关卡设置,只需其中一个,因为核心数据处理另一种方式)。
if ([traps count] != 0)
{
for (NSString* pBounds in traps)
{
NSManagedObject* pNewTrap = [NSEntityDescription insertNewObjectForEntityForName:@"Trap" inManagedObjectContext:m_pMOC];
[pNewTrap setValue:pBounds forKey:@"a_Bounds"];
[pNewTrap setValue:pNewLevel forKey:@"r_Level"];
}
}