简体   繁体   English

比较NSInteger与排序的NSIntegerArray objectAtIndex

[英]compare NSInteger with sorted NSIntegerArray objectAtIndex

For my High score, I need to display the place in the High score in a label before the sorting is initiated. 对于我的高分,我需要在开始排序之前在标签的高分中显示位置。

So I have a highscoreArray at each objectAtIndex is the name and the points saved like this: 所以我在每个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;
}
)

So now for example I finished the game and got 100 points. 因此,举例来说,现在我完成了比赛并获得了100分。 I will get the 3rd place. 我将获得第三名。

How can I compare my score to the highscoreArray and display in a label the objectAtIndex Number? 如何将我的分数与highscoreArray进行比较,并在标签中显示objectAtIndex Number? (in this example: 100 points will be between objectAtIndex: 1 and 2, so I have to be the third place.) (在此示例中:objectAtIndex:1和2之间将为100分,因此我必须排在第三位。)

EDIT: 编辑:

tried: 尝试过:

@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 is an index that iterates through your array, so you don't need all of the separate if statements. i是一个遍历数组的索引,因此您不需要所有单独的if语句。 Note that I have assumed that highscoreindex actually contains NSNumber s because you can't store NSInteger s in an array 请注意,我假设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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM