-2

我是目标 c 的新手,我遇到的问题是,一旦我尝试保存它,我的“scores.xml”就不会被覆盖。我已将“scores.xml”导入我的项目,但一旦我尝试这样做,它就不会被覆盖。我究竟做错了什么?有没有人有任何想法?

- (void) writeHighScores:(NSString *)PlayerName{
NSString *highscorePath = [[NSString alloc]init];
NSArray * paths = [[NSBundle mainBundle] pathsForResourcesOfType: @".xml" inDirectory:@""];
for ( NSString * path in paths )
{
    highscorePath = [path substringToIndex:([path length])];
}

NSMutableArray *arrayFromFile = [NSMutableArray arrayWithContentsOfFile:highscorePath];

for (NSMutableArray *element in arrayFromFile){
    if (score > [[element objectAtIndex:1] intValue]){
        [element  replaceObjectAtIndex:0 withObject:PlayerName];
        [element  replaceObjectAtIndex:1 withObject:[NSString stringWithFormat:@"%d", score]];
        break;
    }
}
NSLog(@"Highscore path = %@",highscorePath);

    // Write array
    [arrayFromFile writeToFile:highscorePath atomically:YES];
}
4

1 回答 1

3

1)您不能写入应用程序包。时期。写入其他地方(例如,写入 Documents 目录)。

2)这段代码应该做什么?这完全没有意义。

NSString *highscorePath = [[NSString alloc]init];
NSArray * paths = [[NSBundle mainBundle] pathsForResourcesOfType: @".xml" inDirectory:@""];
for ( NSString * path in paths )
{
    highscorePath = [path substringToIndex:([path length])];
}
于 2013-02-08T16:56:50.397 回答