存储和读取文档目录iOS 5中的文件

在我的游戏中,当一个关卡完成后,应用程序会在应用程序的Documents目录中的文件中存储一个“1”。 当游戏加载时,如果前一级别已经完成,则玩家只能玩级别。 当我通过Xcode和设备测试游戏时,应用程序正常工作,并且在上一级别完成之前无法播放级别。 但是,当应用程序在App Store上获得批准并发布时,该应用程序的行为就像每个级别都已完成(没有锁定级别)。 我无法想出这一个,并希望得到别人的帮助! 我正在测试的设备都是iOs 5.0或更高版本。

下面是将完成的级别保存在Documents目录中的代码:

NSMutableData* data = [[NSMutableData alloc] init]; NSKeyedArchiver* coder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; NSString *levelString = [NSString stringWithFormat:@"Level%d",level]; [coder encodeInteger:1 forKey:levelString]; [coder finishEncoding]; NSString *levelString2 = [NSString stringWithFormat:@"Level%d.save",level]; /// NSFileManager *filemgr; NSString *dataFile; NSString *docsDir; NSArray *dirPaths; filemgr = [NSFileManager defaultManager]; // Identify the documents directory dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); docsDir = [dirPaths objectAtIndex:0]; // Build the path to the data file dataFile = [docsDir stringByAppendingPathComponent:levelString2]; // Check if the file already exists if ([filemgr fileExistsAtPath: dataFile]) { [[NSFileManager defaultManager] removeItemAtPath:dataFile error:nil]; } [data writeToFile:dataFile atomically:YES]; [coder release]; [data release]; } @catch (NSException* ex) { CCLOG(@"level save failed: %@", ex); } 

下面是读取Document目录以查看级别是否已完成的代码:

 if ([self loadCompletedLevels:6] == 1) { //// level gets unlocked **** } -(int) loadCompletedLevels:(int)theLevel; { int isLevelCompleted; //1 = completed NSString* kSaveFile = [NSString stringWithFormat:@"Level%d.save",theLevel]; NSString *levelString = [NSString stringWithFormat:@"Level%d",theLevel]; @try { NSFileManager *filemgr; NSString *dataFile; NSString *docsDir; NSArray *dirPaths; filemgr = [NSFileManager defaultManager]; // Identify the documents directory dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); docsDir = [dirPaths objectAtIndex:0]; // Build the path to the data file dataFile = [docsDir stringByAppendingPathComponent:kSaveFile]; if ([[NSFileManager defaultManager] fileExistsAtPath:dataFile]) { NSData* data = [[NSData alloc] initWithContentsOfFile:dataFile]; if (data && [data length] > 0) { NSKeyedUnarchiver* decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; isLevelCompleted = [decoder decodeIntForKey:levelString]; [decoder release]; } [data release]; } if (isLevelCompleted == 1) { levelCompleted = YES; } } @catch (NSException* ex) { levelCompleted = NO; } return isLevelCompleted; } 

您应该使用不同的方法来存储数据,但真正的问题是您没有初始化返回值isLevelCompleted。 它在堆栈上,没有默认值。 它始于该堆栈位置发生的任何事情。

因此,如果您不设置它,它将具有任意值。

此外,您应该使用BOOL作为布尔值,但如果您这样做:

 int isLevelCompleted = 0; //1 = completed 

您将它初始化为“false”,因此必须通过代码将其显式更改为“true”。