简体   繁体   English

NSMutableArray / NSMutable Dictionary不在writeToFile上保存数据

[英]NSMutableArray/NSMutable Dictionary not saving data on writeToFile

I have a method that is called addHighScore. 我有一个名为addHighScore的方法。 When a user wants to quit the game, they can save their score out. 当用户想要退出游戏时,他们可以保存他们的分数。 In the resources folder, I created a highScore.plist and populated it with one entry. 在resources文件夹中,我创建了一个highScore.plist并用一个条目填充它。 The structure is: 结构是:

   item 1 (array)
      Name (dictionary, string)
      Level (dictionary, string)
      Score(dictionary, Number)

My problem is this: When I run this in the simulator, after I load the arrHighScores and then add the newScore dictionary, everything seems fine and the records get added (and shown via the NSLog statement) but this is ONLY as long as the app is running. 我的问题是这样的:当我在模拟器中运行它时,在我加载arrHighScores然后添加newScore字典后,一切似乎都很好并且记录被添加(并通过NSLog语句显示)但这只是应用程序在跑。 Once I quit, go back in, the only entry that exists is the one I manually entered. 退出后,返回,唯一存在的条目是我手动输入的条目。

When I run this on the Device (iPhone), it never shows the records added, even while still in the game. 当我在设备(iPhone)上运行它时,它永远不会显示添加的记录,即使仍然在游戏中。 I have looked at just about every example I can concerning NSDictionary and can't seem to figure out what is going wrong. 我已经查看了关于NSDictionary的每个例子,但似乎无法弄清楚出了什么问题。

Any Ideas or suggestions are greatly appreciated. 任何想法或建议都非常感谢。 Thanks in advance for any and all help. 在此先感谢您的帮助。 (Geo...) (GEO ...)

My method looks like this: 我的方法看起来像这样:

 -(IBAction) addHighScore {
    NSString *myPath = [[NSBundle mainBundle] pathForResource:@"highScores" ofType:@"plist"];
    NSLog(@"myPath: %@", myPath);
    NSMutableArray *arrHighScores = [[NSMutableArray alloc] initWithContentsOfFile:myPath];

    NSMutableDictionary *newScore = [[NSMutableDictionary alloc] init];
    [newScore setValue:@"Geo" forKey:@"Name"];
    [newScore setValue:lblLevel.text forKey:@"Level"];
    [newScore setValue:[NSNumber numberWithDouble: dblScore] forKey:@"Score"];

    [arrHighScores addObject:newScore];

    for (int i = 0; i < [arrHighScores count]; i++) {
        NSLog(@"Retreiving (%d) --> %@", i, [arrHighScores objectAtIndex:i]);
    }

    [arrHighScores writeToFile:myPath atomically:YES];

    NSMutableArray *tmpArray2 = [[NSMutableArray alloc] initWithContentsOfFile:myPath];

    NSSortDescriptor *mySorter = [[NSSortDescriptor alloc] initWithKey:@"Score" ascending:YES];
    [tmpArray2 sortUsingDescriptors:[NSArray arrayWithObject:mySorter]];    

    for (int i = 0; i < [tmpArray2 count]; i++) {
        NSLog(@"Retreiving (%d) --> %@", i, [tmpArray2 objectAtIndex:i]);
    }

    [arrHighScores release];
    [tmpArray2 release];
    [mySorter release];
    [newScore release]; 

}

The problem is that you are attempting to save your changes in application bundle but you don't have permissions to write there. 问题是您试图在应用程序包中保存更改,但您没有权限在那里写。 The correct place to save data is Documents directory in application sandbox - you can get it path: 保存数据的正确位置是应用程序沙箱中的Documents目录 - 您可以获取它的路径:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                             NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];

So the correct workflow should be: 所以正确的工作流程应该是:

  1. Try to load highscores data from Documents folder 尝试从Documents文件夹加载高分数据
  2. If file in Documents folder does not exist load data from resources 如果文档文件夹中的文件不存在则从资源加载数据
  3. Update data 更新数据
  4. Save data to Documents folder 将数据保存到Documents文件夹

See also "Commonly Used Directories" for more information about where you can and should save you application data. 另请参阅“常用目录”以获取有关可以和应该保存应用程序数据的位置的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM