How to compare two NSInteger?

How do we compare two NSInteger numbers? I have 2 NSIntegers and compare them regularly the way that didn't work.

if (NSIntegerNumber1 >= NSIntegerNumber2) { //do something } 

Although the first value was 13 and the second value is 17, the if loop does

Any idea?

+6
source share
4 answers
 NSInteger int1; NSInteger int2; int1 = 13; int2 = 17; if (int1 > int2) { NSLog(@"works"); } 
+9
source

Well, since you have Integer and Number in the name, you could declare two values ​​as NSNumber instead of NSInteger. If so, you need to do the following:

  if ([NSIntegerNumber1 intValue] >= [NSIntegerNumber2 intValue]) { // do something } 

Otherwise, it should work as it is!

+11
source

NSInteger is just a typedef for a built-in integral type (e.g. int or long ).

It is safe to compare with a == b .

Other common operators behave predictably != , <= , < , >= , Etc.

Finally, the NSInteger base type is platform / architecture dependent. It is unsafe to assume that it will always be 32 or 64 bits.

+7
source

When comparing integers using this will work fine:

 int a = 5; int b = 7; if (a < b) { NSLog(@"%d is smaller than %d" a, b); } 
+2
source

Source: https://habr.com/ru/post/909514/


All Articles