我想出了一个显示游戏高分的想法,而我实际上想要做的基本上是将两个数字添加到 anNSMutableArray
并检查两个数字之间的最大值,阵列中最大的停留在另一个必须删除。
@property (strong, nonatomic) NSMutableArray *highscoreArray;
-(void)displaHighScorelabel:(int)score
{
NSNumber *point = [NSNumber numberWithInt:score];
[self.highscoreArray addObject:point];
int highscore = 0;
if([self.highscoreArray indexOfObject:[self.highscoreArray lastObject]] == 0)
highscore = [[self.highscoreArray lastObject]integerValue];
else for(int i=0; i < [self.highscoreArray count];i++)
{
if ([self.highscoreArray[i] intValue] >= [self.highscoreArray[i+1] intValue]) {
[self.highscoreArray removeObjectAtIndex:i+1];
highscore = [self.highscoreArray[i] intValue];
}
else if([self.highscoreArray[i] intValue] <= [self.highscoreArray[i+1] intValue])
{
[self.highscoreArray removeObjectAtIndex:i];
highscore = [self.highscoreArray[i+1] intValue];
}
}
self.highscoreLabel.text = [NSString stringWithFormat:@"High Score: %i",highscore];
[self CheckSomething:self.highscoreArray];
}
问题是:数组在堆中只保留一个数字。我怎样才能保留它而不释放它?由于我要添加的下一个数字将位于索引 0 处,这基本上意味着前一个数字已从堆中释放。