Iphone NSMUTABLEARRY/NSMutable Dictionary未将数据保存在writeToFile上

Iphone NSMUTABLEARRY/NSMutable Dictionary未将数据保存在writeToFile上,iphone,xcode,Iphone,Xcode,我有一个叫做addHighScore的方法。当用户想要退出游戏时,他们可以保存他们的分数。在resources文件夹中,我创建了一个highScore.plist并用一个条目填充它。结构如下: item 1 (array) Name (dictionary, string) Level (dictionary, string) Score(dictionary, Number) 我的问题是:当我在模拟器中运行这个程序时,在加载arrHighScores

我有一个叫做addHighScore的方法。当用户想要退出游戏时,他们可以保存他们的分数。在resources文件夹中,我创建了一个highScore.plist并用一个条目填充它。结构如下:

   item 1 (array)
      Name (dictionary, string)
      Level (dictionary, string)
      Score(dictionary, Number)
我的问题是:当我在模拟器中运行这个程序时,在加载arrHighScores并添加newScore字典之后,一切看起来都很好,记录也被添加了(并通过NSLog语句显示),但这只是应用程序运行的时间。一旦我退出,返回,唯一存在的条目就是我手动输入的条目

当我在设备(iPhone)上运行此功能时,它从不显示添加的记录,即使在游戏中。我看了几乎每一个例子,我可以在字典里,似乎不能找出什么是错的

非常感谢您的任何想法或建议。提前感谢您的帮助。(地理…)

我的方法如下所示:

 -(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]; 

}
-(iAction)添加高分{
NSString*myPath=[[NSBundle mainBundle]pathForResource:@“highScores”类型:@“plist”];
NSLog(@“myPath:%@”,myPath);
NSMutableArray*arrhightscores=[[NSMutableArray alloc]initWithContentsOfFile:myPath];
NSMutableDictionary*newScore=[[NSMutableDictionary alloc]init];
[newScore setValue:@“Geo”forKey:@“Name”];
[newScore setValue:lblLevel.text forKey:@“Level”];
[newScore setValue:[NSNumber numberwhithdouble:dblScore]forKey:@“Score”];
[arrHighScores addObject:newScore];
对于(int i=0;i<[arrHighScores count];i++){
NSLog(@“检索(%d)-->%@),i[arrhightscores objectAtIndex:i]);
}
[arrhightscoreswritetofile:myPath原子:YES];
NSMutableArray*tmpArray2=[[NSMutableArray alloc]initWithContentsOfFile:myPath];
NSSortDescriptor*mySorter=[[NSSortDescriptor alloc]initWithKey:@“分数”递增:是];
[tmpArray2排序描述符:[NSArray arrayWithObject:mySorter]];
对于(int i=0;i<[tmpArray2计数];i++){
NSLog(@“检索(%d)-->%@),i[tmpArray2 objectAtIndex:i]);
}
[高分发布];
[tmpArray2发布];
[我的分拣机释放];
[新闻核心发布];
}

问题在于,您正试图将更改保存到应用程序包中,但您没有在其中写入的权限。保存数据的正确位置是应用程序沙箱中的Documents目录-您可以获得它的路径:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                             NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];
因此,正确的工作流程应该是:

  • 尝试从“文档”文件夹加载高分数据
  • 如果文档文件夹中的文件不存在,请从资源加载数据
  • 更新数据
  • 将数据保存到文档文件夹

  • 另请参阅,以了解有关可以和应该将应用程序数据保存在何处的更多信息。

    问题在于,您正试图将更改保存在应用程序包中,但您没有在其中写入的权限。保存数据的正确位置是应用程序沙箱中的Documents目录-您可以获得它的路径:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                 NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    因此,正确的工作流程应该是:

  • 尝试从“文档”文件夹加载高分数据
  • 如果文档文件夹中的文件不存在,请从资源加载数据
  • 更新数据
  • 将数据保存到文档文件夹
  • 另请参阅,以了解有关可以和应该在何处保存应用程序数据的更多信息