Reputation: 850
Im trying to find a faster way to check for multiple integers. This works but will be massive. Im new and teaching myself ObjC, any help would be great.
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];
}
Upvotes: 1
Views: 45
Reputation: 12753
One way to shorten your code is to place the numbers into an array and then compare each array element against the score in a loop. Here's an example of how to do that:
Create and initialize an NSArray. Each element of the array is an NSNumber.
NSArray *array = @[@2, @17, @33, @42, @52, @65, @85, @101, @125, @139];
Compare each element of the array against the score in a loop. Exit the loop if a match is found.
for (NSNumber *number in array) {
if ([_scoreLabel.text intValue] == number.intValue) {
[self setupNode];
break;
}
}
Upvotes: 1
Reputation: 46578
improvement over @0x141E's answer
NSArray *array = @[@2, @17, @33, @42, @52, @65, @85, @101, @125, @139];
if ([array containsObject:@(_scoreLabel.text intValue)]) {
[self setupNode];
}
Upvotes: 2