Для моего высокого балла мне нужно отобразить место в верхнем байте на ярлыке до начала сортировки.сравнить NSInteger с отсортированным NSIntegerArray objectAtIndex
Так у меня есть highscoreArray на каждом objectAtIndex это имя, и точки, сохраненные как это:
2015-06-29 03:08:34.159 Game[3408:838910] (
{
name = "TEST-TES";
points = 250;
},
{
name = EEE;
points = 180;
},
{
name = HHHHH;
points = 90;
},
{
name = "ADTES";
points = 70;
},
{
name = EE;
points = 70;
},
{
name = AAAAAAAAAAAAAAA;
points = 70;
},
{
name = TEEEST;
points = 40;
},
{
name = R;
points = 20;
},
{
name = er;
points = 20;
},
{
name = test1;
points = 20;
}
)
Так что теперь, например, я закончил игру и получил 100 баллов. Я получу 3-е место.
Как я могу сравнить свою оценку с highscoreArray и отобразить на этикетке номер объектаAtIndex? (В этом примере: 100 точек будет находиться в диапазоне от objectAtIndex: 1 и 2, так что я должен быть третье место.)
РЕДАКТИРОВАТЬ:
пытался:
@property (nonatomic) NSMutableArray *highscoreindex;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
_highscoreindex = [defaults objectForKey:@"highscore"];
NSLog(@"%@", _highscoreindex);
int i;
UIColor *color = [UIColor blackColor];
for (i = 0; i < [_highscoreindex count]; i++) {
[_highscoreindex objectAtIndex:i];
if ([_highscoreindex objectAtIndex:0 < self.score]) {
_highscoreIndexLabel.text = [NSString stringWithFormat:@"1"];
[_highscoreIndexLabel setTextAlignment:NSTextAlignmentCenter];
[_highscoreIndexLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:80]];
[_highscoreIndexLabel setTextColor:color];
}
if ([_highscoreindex objectAtIndex:0 > self.score]) {
_highscoreIndexLabel.text = [NSString stringWithFormat:@"2"];
[_highscoreIndexLabel setTextAlignment:NSTextAlignmentCenter];
[_highscoreIndexLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:80]];
[_highscoreIndexLabel setTextColor:color];
}
if ([_highscoreindex objectAtIndex:1 > self.score]) {
_highscoreIndexLabel.text = [NSString stringWithFormat:@"3"];
[_highscoreIndexLabel setTextAlignment:NSTextAlignmentCenter];
[_highscoreIndexLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:80]];
[_highscoreIndexLabel setTextColor:color];
}
if ([_highscoreindex objectAtIndex:2 > self.score]) {
_highscoreIndexLabel.text = [NSString stringWithFormat:@"4"];
[_highscoreIndexLabel setTextAlignment:NSTextAlignmentCenter];
[_highscoreIndexLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:80]];
[_highscoreIndexLabel setTextColor:color];
}
if ([_highscoreindex objectAtIndex:3 > self.score]) {
_highscoreIndexLabel.text = [NSString stringWithFormat:@"5"];
[_highscoreIndexLabel setTextAlignment:NSTextAlignmentCenter];
[_highscoreIndexLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:80]];
[_highscoreIndexLabel setTextColor:color];
}
if ([_highscoreindex objectAtIndex:4 > self.score]) {
_highscoreIndexLabel.text = [NSString stringWithFormat:@"6"];
[_highscoreIndexLabel setTextAlignment:NSTextAlignmentCenter];
[_highscoreIndexLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:80]];
[_highscoreIndexLabel setTextColor:color];
}
if ([_highscoreindex objectAtIndex:5 > self.score]) {
_highscoreIndexLabel.text = [NSString stringWithFormat:@"7"];
[_highscoreIndexLabel setTextAlignment:NSTextAlignmentCenter];
[_highscoreIndexLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:80]];
[_highscoreIndexLabel setTextColor:color];
}
if ([_highscoreindex objectAtIndex:6 > self.score]) {
_highscoreIndexLabel.text = [NSString stringWithFormat:@"8"];
[_highscoreIndexLabel setTextAlignment:NSTextAlignmentCenter];
[_highscoreIndexLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:80]];
[_highscoreIndexLabel setTextColor:color];
}
if ([_highscoreindex objectAtIndex:7 > self.score]) {
_highscoreIndexLabel.text = [NSString stringWithFormat:@"9"];
[_highscoreIndexLabel setTextAlignment:NSTextAlignmentCenter];
[_highscoreIndexLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:80]];
[_highscoreIndexLabel setTextColor:color];
}
if ([_highscoreindex objectAtIndex:8 > self.score]) {
_highscoreIndexLabel.text = [NSString stringWithFormat:@"10"];
[_highscoreIndexLabel setTextAlignment:NSTextAlignmentCenter];
[_highscoreIndexLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:80]];
[_highscoreIndexLabel setTextColor:color];
}
}
Вы должны перебирать массив, сравнивая значение в каждой словарной записи разорвать петлю в соответствующей точке , Можете ли вы показать код, который вы пробовали? – Paulw11
Вы должны передать переменную i в objectAtIndex: вместо 0,1,2 ... и ваш блок «больше ли это?»? код должен быть записан только один раз в теле цикла. То, как это происходит сейчас, поскольку в массиве много элементов, он будет сравнивать первые 9 элементов массива с self.score и многократно менять метку. –