对于我的高分,我需要在开始排序之前在标签的高分中显示位置。
所以我在每个objectAtIndex都有一个highscoreArray是这样保存的名称和要点:
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分。我将获得第三名。
如何将我的分数与highscoreArray进行比较,并在标签中显示objectAtIndex Number? (在此示例中:objectAtIndex:1和2之间将为100分,因此我必须排在第三位。)
编辑:
尝试过:
@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];
}
}
最佳答案
i
是一个遍历数组的索引,因此您不需要所有单独的if语句。请注意,我假设highscoreindex
实际上包含NSNumber
,因为您不能将NSInteger
存储在数组中
@property (nonatomic) NSMutableArray *highscoreindex;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.highscoreindex = [defaults objectForKey:@"highscore"];
int i;
UIColor *color = [UIColor blackColor];
for (i = 0; i < self.highscoreindex.count; i++) {
NSDictionary *highScoreDict=(NSDictionary *)self.highscoreindex[i]
NSInteger highScore=[(NSNumber *)highScoreDict[@"points"] integerValue];
if (self.score > highScore) {
self.highscoreIndexLabel.text = [NSString stringWithFormat:@"%d",i+1];
[self.highscoreIndexLabel setTextAlignment:NSTextAlignmentCenter];
[self.highscoreIndexLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:80]];
[self.highscoreIndexLabel setTextColor:color];
break;
}
}
关于ios - 比较NSInteger与排序的NSIntegerArray objectAtIndex,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31106481/