Comparing NSNumbers in Objective C

Objective CNsnumber

Objective C Problem Overview


I am a beginner at Objective-C and I am a bit confused at this scenario. I have the following code:

if (number1 < number2) {
    NSLog(@"THE FOLLOWING NUMBER ");
    NSLog(@"%@", number1);
    NSLog(@"IS LESS THAN");
    NSLog(@"%@", number2);
}

When I run this code I see really strange results like this:

2011-07-06 20:38:18.044 helloworld[1014:207] THE FOLLOWING NUMBER 
2011-07-06 20:38:18.047 helloworld[1014:207] 190.8776
2011-07-06 20:38:18.050 helloworld[1014:207] IS LESS THAN
2011-07-06 20:38:18.053 helloworld[1014:207] 96.75866

Both numbers are NSNumber objects, how could something like this happen? I am getting the two numbers by finding distances between sprites on the screen.

Any clues or advice would really be appreciated

Objective C Solutions


Solution 1 - Objective C

I assume number1 and number2 are pointers to objects. The < sign is comparing the pointers.

You need to compare the actual floatValue or doubleValue

if ([number1 doubleValue] < [number2 doubleValue]) 

....

Solution 2 - Objective C

For cases where you simply want to test whether two NSNumber properties hold the same value, then from the Apple documentation it seems that using

- (BOOL)isEqualToNumber:(NSNumber *)aNumber

is the most straightforward and efficient way to compare two NSNumber values.

For example:

if ([someNumber isEqualToNumber:someOtherNumber])
{
    // The numbers hold the same value
}
else
{
    // The numbers hold different values
}

The documentation also says "This method is more efficient than compare: if you know the two objects are numbers."

Whenever you need to know whether a value is smaller or greater, they offer the

- (NSComparisonResult)compare:(NSNumber *)aNumber

method, but personally I would prefer at that point to just pull out the integer values (or double values) and use regular < and > operators to do the comparison, because that makes the code much easier to read, like so:

if (firstNumber.intValue > secondNumber.intValue)
{
    // First number is greater than the second number
}
else if (firstNumber.intValue == secondNumber.intValue)
{
    // The two numbers have the same value
}
else
{
    // The first number is smaller than the second number
}

Something like that is much easier to read than calls to -compare:, in my opinion.

Erik

Solution 3 - Objective C

NSNumber has a method for comparison: - (NSComparisonResult)compare:(NSNumber)aNumber*

if([numberOne compare:numberTwo] == NSOrderedSame) 
{
      // proceed
}

Solution 4 - Objective C

Swift 3.1

let number1 = NSNumber(value: 10.2)
let number2 = NSNumber(value: 20.2)
let result = number1.compare(number2)
if  result == .orderedAscending {
    
} else if result == .orderedDescending {
    
} else { // .orderedSame
    
}

Solution 5 - Objective C

These work great:

if ([number1 isLessThan:number2])

if ([number1 isLessThanOrEqualTo:number2])

if ([number1 isGreaterThan:number2])

if ([number1 isGreaterThanOrEqualTo:number2])

if ([number1 isEqualTo:number2])

if ([number1 isNotEqualTo:number2])

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDoug MolineuxView Question on Stackoverflow
Solution 1 - Objective CKalView Answer on Stackoverflow
Solution 2 - Objective CErik van der NeutView Answer on Stackoverflow
Solution 3 - Objective CJastinBallView Answer on Stackoverflow
Solution 4 - Objective CWilliam HuView Answer on Stackoverflow
Solution 5 - Objective CAgentRevView Answer on Stackoverflow