在iOS中保存简单值会导致大型文档和数据文件

时间:2014-06-17 11:47:14

标签: ios nsdocumentdirectory

我正在开发一个简单的iOS游戏,我的应用程序大小只有3 MB 这是我用来保存用户高分的代码

问题是,每次我使用该应用程序时,其文档和数据文件的大小都会增加。它在我的iPad上达到了28 MB。为什么会这样? 任何帮助表示赞赏

     - (void)applicationDidEnterBackground:(UIApplication *)application{
NSArray *value = [[NSArray alloc]initWithObjects:[NSNumber numberWithFloat:easyHighscoreFloat],[NSNumber numberWitFloat:normalHighscoreFloat],[NSNumber numberWithFloat:hardHighscoreFloat],nil];
[value writeToFile:[self saveHighScore] atomically:YES];
}




    -(NSString*)saveHighScore{
NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[filePath objectAtIndex:0]stringByAppendingPathComponent:@"savedState.plist"];
 }




- (void)viewDidLoad{

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:[UIApplication sharedApplication]];

     NSString *path = [self saveHighScore];
BOOL fileExists = [[NSFileManager defaultManager]fileExistsAtPath:path];

if (fileExists) {

    NSArray *loadValues = [[NSArray alloc]initWithContentsOfFile:path];
    easyHighscoreFloat = [[loadValues objectAtIndex:0]floatValue];
    normalHighscoreFloat = [[loadValues objectAtIndex:1]floatValue];
    hardHighscoreFloat = [[loadValues objectAtIndex:2]floatValue];

}else{
    easyHighscoreFloat = 0;
    normalHighscoreFloat = 0;
    hardHighscoreFloat = 0;
    }
}

0 个答案:

没有答案