我有这个方法f=open('c:\Lets_Create_Malware\output.txt', 'r+')
f=open('c:\Lets_Create_Malware\output.txt', 'w+')
f=open('c:\Lets_Create_Malware\output.txt', 'a+')
f=open('c:\Lets_Create_Malware\output.txt', 'r')
f=open('c:\Lets_Create_Malware\output.txt', 'w')
f=open('c:\Lets_Create_Malware\output.txt', 'a')
,它向我的核心数据存储发送一个获取请求,以根据两个参数,级别和级别类型(级别)获取有关此getLevelInformation
int
的信息是0-99中的任何_bestMoveCount
,以及1-4中的等级类型。如果未找到谓词,则会为特定级别和最佳移动创建新对象(如果尚未创建级别,则创建级别类型)。否则,我会检索最佳动作int
并最终将其转换为我的NSNumber
实例var NSInteger
。
我的问题:当我重播我的某个级别时,_bestMoveCount
(当它检查特定提取中是否包含任何内容时)似乎总是空的(它不应该是#t} ,因为我保存了新创建的数据)。当我再次回到该级别并在_levelInformation
语句处设置断点时,我会在if
方法之后查看_levelInformation
。这就是我所看到的:
executeFetchRequest:
此时,如果我点击了第2级, <Score: 0x1cd68a40> (entity: Score; id: 0x1cd68470 <x-coredata:///Score/t510F3599-89E9-4A57-B9BC-34DE0131E2142> ; data: {
bestMovement = 0;
bestTime = 0;
level = 0;
overallScore = 0;
typeOfLevel = 0;
})
应为18,bestMovement
应为1,level
应为2(我正在玩我的第二种类型)。有关为什么这不能保存我的数据的任何想法?
typeOfLevel
忘了提。完成级别后,我再次保存新数据。
- (void)getLevelInformation {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
_managedObjectContext = [(AppDelegate*)[[UIApplication sharedApplication]
delegate] managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Score" inManagedObjectContext:_managedObjectContext];
[fetchRequest setEntity:entityDescription];
NSNumber* levelNumber = [NSNumber numberWithInteger:_currentLevel];
NSNumber* typeNumber = [NSNumber numberWithInteger:_level];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@ AND %K == %@",
@"level", levelNumber, @"typeOfLevel", typeNumber];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
_levelInformation = [[_managedObjectContext
executeFetchRequest:fetchRequest error:&error] lastObject];
// the only way this fails is if the level the player is on has never been accessed
// OR RATHER, if the player never actually finished the level (thus never getting
// a best move count, best time, and overall score.
if (!_levelInformation) {
_levelInformation = [NSEntityDescription insertNewObjectForEntityForName:@"Score"
inManagedObjectContext:_managedObjectContext];
[_levelInformation setValue:levelNumber forKey:@"level"];
if (![_levelInformation valueForKey:@"typeOfLevel"]) {
[_levelInformation setValue:typeNumber forKey:@"typeOfLevel"];
}
[_levelInformation setValue:[NSNumber numberWithInteger:0] forKey:@"bestMovement"];
if (![_levelInformation.managedObjectContext save:&error]) {
NSLog(@"Unable to save managed object context.");
NSLog(@"%@, %@", error, error.localizedDescription);
}
}
_levelInformation.bestMovement = [_levelInformation valueForKey:@"bestMovement"];
_bestMoveCount = [_levelInformation.bestMovement integerValue];
}