Reputation: 2982
How do we compare two NSInteger numbers ? I have two NSIntegers and comparing them the regular way wasnt working.
if (NSIntegerNumber1 >= NSIntegerNumber2) {
//do something
}
Eventhough, the first value was 13 and the second value was 17, the if loop is executing
Any idea ?
Upvotes: 8
Views: 32288
Reputation: 1033
NSInteger int1;
NSInteger int2;
int1 = 13;
int2 = 17;
if (int1 > int2)
{
NSLog(@"works");
}
Upvotes: 9
Reputation: 104728
NSInteger
is just a typedef for a builtin integral type (e.g. int
or long
).
It is safe to compare using a == b
.
Other common operators behave predictably: !=
, <=
, <
, >=
et al.
Finally, NSInteger
's underlying type varies by platform/architecture. It is not safe to assume it will always be 32 or 64 bit.
Upvotes: 13
Reputation: 488
When comparing integers, using this, would work just fine:
int a = 5;
int b = 7;
if (a < b) {
NSLog(@"%d is smaller than %d" a, b);
}
Upvotes: 2
Reputation: 25740
Well, since you have Integer and Number in the name, you might have declared the two values as NSNumber instead of NSInteger. If so, then you need to do the following:
if ([NSIntegerNumber1 intValue] >= [NSIntegerNumber2 intValue]) {
// do something
}
Otherwise it should work as is!
Upvotes: 12