【问题标题】:compare NSInteger with sorted NSIntegerArray objectAtIndex将 NSInteger 与排序的 NSIntegerArray objectAtIndex 进行比较
【发布时间】: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


【解决方案1】:

i 是一个遍历数组的索引,因此您不需要所有单独的 if 语句。请注意,我假设 highscoreindex 实际上包含 NSNumbers,因为您不能将 NSIntegers 存储在数组中

@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;
    }
}

【讨论】:

  • 我一直喜欢你的回答
  • 谢谢,但我收到此错误:2015-06-29 12:12:45.132 Game[17906:585056] -[__NSCFDictionary integerValue]: unrecognized selector sent to instance 0x7feba0707a70 2015-06-29 12 :12:45.156 Game[17906:585056] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFDictionary integerValue]:无法识别的选择器发送到实例 0x7feba0707a70”
  • 我认为问题在于,_highscoreIndex 中的每个 objectAtIndex 名称和点数,所以他无法比较?
  • _highscoreIndex NSLog: 2015-06-29 12:29:42.230 Game[18007:590105] ({ name = EEEE; points = 70; } ) 所以高分条目看起来像
  • 我的答案基于您的代码,但似乎数组中的每个元素都是字典,而不是您的代码建议的整数。我会更新我的答案
猜你喜欢
  • 2014-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
  • 1970-01-01
  • 2015-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多