所以,我有一个数组,我从一个名为“choice”的文件中检索它。我的问题是每次我去高分页面,它只显示最高的高分。此外,当我下次加载它时,它只保留数组中的两个元素。这是我的代码:
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];
}