For my next app update I want to add a highscore. The code bit I have for saving the score is as follows:
For some odd reason score2 always returns 0, yet [time2 text] is the correct score. I am completely baffled. Am I misusing "intvalue"? Any help or advice would be greatly appreciated! Edit: I recently looked at the NSString class reference provided by apple, and it said intvalue "Returns 0 if the receiver doesn’t begin with a valid decimal text representation of a number". I think this means that if the string doesn't start with a number it won't work, but that doesn't make sense because the value I wrote to the file does start with a number.
Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFileName = [NSString stringWithFormat:@"%@/highscore", documentsDirectory];
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];
NSString *score=[array objectAtIndex:0];
[time2 setText:score];
int score2=[[time2 text] intValue];
if (points>score2){
//Points is the
//amount of points
//just scored
NSMutableArray *array2=array;
[array2 removeObjectAtIndex:0];
[array2 insertObject:[NSString stringWithFormat:@"%i",points] atIndex:0];
printf("High score beat! New score is:%i Old score was:%i",points,score2);
[array2 writeToFile:fullFileName atomically:NO];
}