-2

So, I have an array which I retrieve from a file named "choice". My problem is that every time I go to the highscore page, it only displays the highest high score. Also, it only keeps two of the elements from array when I load it next time. Here is my code:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *choice = [NSString stringWithFormat:@"%@/userschoice", documentsDirectory];
NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:choice];
NSArray *sortedHighScores = [array sortedArrayUsingSelector:@selector(compare:)];
NSMutableArray *finalArray = [[NSMutableArray alloc] init];



if ([array count]!= 0)
{   
    [array removeAllObjects];


    for (id someObject in [sortedHighScores reverseObjectEnumerator])
    {
        [finalArray addObject:someObject];

    }

    if ([array count]>1){
        NSNumber *highscore3 = [finalArray objectAtIndex:1];
        highscore2Label.text = [NSString stringWithFormat:@"2. %@ seconds",[highscore3 stringValue]];
        [array addObject:highscore3];
    }
    if ([array count] > 2){
         NSNumber *highscore4 = [finalArray objectAtIndex:2];
         highscore3Label.text = [NSString stringWithFormat:@"3. %@ seconds",[highscore4 stringValue]];
         [array addObject:highscore4];

    }

    if ([array count] > 3){
        NSNumber *highscore5 = [finalArray objectAtIndex:3];
        highscore4Label.text = [NSString stringWithFormat:@"4. %@ seconds",[highscore5 stringValue]];
        [array addObject:highscore5];

    }



    if ([array count] > 4){
        NSNumber *highscore1 = [finalArray objectAtIndex:4];
        highscore5Label.text = [NSString stringWithFormat:@"5. %@ seconds",[highscore1 stringValue]];
        [array addObject:highscore1];
    }








    NSNumber *highscore2 = [finalArray objectAtIndex:0];
    highscore1Label.text = [NSString stringWithFormat:@"1. %@ seconds", [highscore2 stringValue]];
    [array addObject:highscore2];

    [array writeToFile:choice atomically:YES];
}
  • You try to access objects from `finalArray` before you put anything in that array. – rmaddy Sep 04 '13 at 23:34
  • @rmaddy I fixed that but now it just displays only one of the highscores and the array only stores two highscores (see edited description for further explanation) – user2607954 Sep 05 '13 at 00:34

1 Answers1

0

There's this line:

NSMutableArray *finalArray = [[NSMutableArray alloc] init];

....followed shortly by this one:

NSNumber *highscore2 = [finalArray objectAtIndex:0];

After the first of these lines, finalArray is a mutable array that contains no objects. On the second of these lines, you try to look up the object at index 0. Except that you haven't added any objects to finalArray, so there is no object at index 0. That's illegal, so you get an exception and a crash. I'm not sure what you're trying to do with finalArray, but you can't look up objects in an array unless it actually contains some objects. You should check the array count before doing the lookup.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • I fixed that and now I get this error:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]' *** First throw call stack: (0x18e7012 0x12cce7e 0x18890b4 0xa387 0x4e51c7 0x4e5232 0x4f0c25 0x6f03a3 0x4edee3 0x4ee167 0x898e 0x12e0705 0x4072c0 0x407258 0x4c8021 0x4c857f 0x4c76e8 0x436cef 0x436f02 0x414d4a 0x406698 0x2222df9 0x2222ad0 0x185cbf5 0x185c962 0x188dbb6 0x188cf44 0x188ce1b 0x22217e3 0x2221668 0x403ffc 0x1d32 0x1c65) libc++abi.dylib: terminate called throwing an exception – user2607954 Sep 04 '13 at 23:43
  • I fixed that problem. Now whenever I click the highscores page, it only displays the highest highscore and it removes all but two elements from the array named array. – user2607954 Sep 04 '13 at 23:51
  • Whenever you try access to objects in finalArray, perform a check against the index of object you want to access vs the size of finalArray. This can help you to figure out where is the error and prevent further halts – Black Maggie Sep 05 '13 at 02:23
  • @BlackMaggie I fixed the problem. The problem was that I cleared the array named "array", and then tried to add objects to the array based on the amount of objects in it (which would be therefore always zero). After I fixed this, it ran perfectly. – user2607954 Sep 06 '13 at 00:03