-2

したがって、「choice」という名前のファイルから取得する配列があります。私の問題は、ハイスコア ページに移動するたびに、最高のハイ スコアしか表示されないことです。また、次回ロードするときに、配列から 2 つの要素のみが保持されます。これが私のコードです:

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

1 に答える 1