gpt4 book ai didi

ios - 将 NSInteger 与排序后的 NSIntegerArray objectAtIndex 进行比较

转载 作者:行者123 更新时间:2023-11-28 21:43:43 25 4
gpt4 key购买 nike

对于我的高分,我需要在开始排序之前在标签中显示高分中的位置。

所以我在每个 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 数字? (在这个例子中: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];
}

}

最佳答案

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com