Question

I'm new to objective c and the problem I'm having is that my "scores.xml" is not overwritten once I try to save it. I have imported the "scores.xml" to my project, but it does not get overwritten once I try to do so. What am I doing wrong? Does anyone have any ideas?

- (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];
}
Was it helpful?

Solution

1) You can't write to the app bundle. Period. Write to somewhere else (for example, to the Documents directory).

2) What is this code supposed to do? It makes absolutely no sense.

NSString *highscorePath = [[NSString alloc]init];
NSArray * paths = [[NSBundle mainBundle] pathsForResourcesOfType: @".xml" inDirectory:@""];
for ( NSString * path in paths )
{
    highscorePath = [path substringToIndex:([path length])];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top