Objective c Sprite套件中更短/更快的方法

Objective c Sprite套件中更短/更快的方法,objective-c,if-statement,Objective C,If Statement,我想找到一种更快的方法来检查多个整数。这是可行的,但将是巨大的。我是新来的,正在自学ObjC,任何帮助都会很好 if ([_scoreLabel.text intValue]== 2|| [_scoreLabel.text intValue]==17|| [_scoreLabel.text intValue]==33|| [_scoreLabel.text intValue]==42 || [_scoreLabel.text intValue]==52|| [_scoreLabel.text i

我想找到一种更快的方法来检查多个整数。这是可行的,但将是巨大的。我是新来的,正在自学ObjC,任何帮助都会很好

if ([_scoreLabel.text intValue]== 2||
[_scoreLabel.text intValue]==17||
[_scoreLabel.text intValue]==33||
[_scoreLabel.text intValue]==42 ||
[_scoreLabel.text intValue]==52||
[_scoreLabel.text intValue]==65 ||
[_scoreLabel.text intValue]==85 ||
[_scoreLabel.text intValue]==101 ||
[_scoreLabel.text intValue]==125 ||
[_scoreLabel.text intValue]==139)
{
[self setupNode];
}

缩短代码的一种方法是将数字放入数组,然后将每个数组元素与循环中的分数进行比较。下面是一个如何做到这一点的示例:

创建并初始化NSArray。数组的每个元素都是一个NSNumber

NSArray *array = @[@2, @17, @33, @42, @52, @65, @85, @101, @125, @139];
将数组的每个元素与循环中的分数进行比较。如果找到匹配项,则退出循环

for (NSNumber *number in array) {
    if ([_scoreLabel.text intValue] == number.intValue) {
        [self setupNode];
        break;
    }
}

对@0x141E答案的改进

NSArray *array = @[@2, @17, @33, @42, @52, @65, @85, @101, @125, @139];
if ([array containsObject:@(_scoreLabel.text intValue)]) {
    [self setupNode];
}

这里有什么图案吗。这段代码要解决什么问题?没有模式。我只是想把这个缩短。我希望在一个字符串中包含1000个左右的整数,但我尝试过的所有方法都会出错。这些数字有什么特别之处?没有什么特别之处。我只是在玩家命中特定分数时运行操作。这不会提高性能,但你可以将数字保留在一个数组中,然后检查scoreLabel.text是否在其中。效果很好!非常感谢。非常有帮助!