【发布时间】:2015-06-29 01:17:44
【问题描述】:
对于我的高分,我需要在开始排序之前在标签中显示高分中的位置。
所以我在每个 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 传递给 objectAtIndex: 而不是 0,1,2... 并且您的块“它更大吗?它更少吗?”代码只能在循环体中编写一次。现在的方式是,无论数组中有多少项,它都会将前 9 个数组项与 self.score 进行比较并多次更改标签。
标签: ios objective-c string-comparison