-2

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];
}
Rokas
  • 57
  • 1
  • 5
  • Add code to validate the return value; `if (![arrayFromFile writeToFile:...]) { NSLog(@"writeToFile failed!"); }`. One reason for this failure is that the array doesn't contain just *property list objects* (see the reference for details). – trojanfoe Feb 08 '13 at 16:52
  • @trojanfoe or perhaps that you can't just write files here and there, as OP thinks. –  Feb 08 '13 at 16:57
  • But again, [this has been asked many, many times...](http://stackoverflow.com/questions/5853014/writing-into-a-file-objective-c) –  Feb 08 '13 at 16:58
  • @H2CO3 Ah, I missed that. – trojanfoe Feb 08 '13 at 19:12
  • @trojanfoe No worries mate. This question is just so wrong that it isn't even worth arguing about. –  Feb 08 '13 at 19:14

1 Answers1

3

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])];
}