objective c:无法用数组覆盖文件

时间:2013-02-08 16:47:09

标签: objective-c arrays cocoa writetofile

我是目标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];
}

1 个答案:

答案 0 :(得分: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])];
}