Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare NSNumber with NSInteger

I spent some time today chasing down two bugs, and ended up fixing both of them using the same solution.

Now that I have the solution, I was hoping to get some clarity behind it.

I'm comparing an attribute from Core Data (Integer 16/NSNumber) with an Integer (ABPropertyID & ABMultiValueIdentifier).

The bug was in this comparison, and oddly enough, only showed itself after I had killed the app (from the background tray), reopened it, and run through the same process that included the comparison. Anyways...

This is what stopped working after a restart:

if (myNumber.aProperty == [NSNUmber numberWithInt:anInteger]) { /* do stuff here */ }

And these are the two solutions, which so far, are working perfectly:

if ([myNumber.aProperty integerValue] == anInteger) {/* do stuff here */ }

if ([myNumber.aProperty isEqualToNumber:[NSNumber numberWithInt:anInteger]]) { /* do stuff here */ }

To me, they all look identical. I'm always either converting the NSNumber to an integerValue, or converting the integer to an NSNumber.

Any ideas?

like image 233
djibouti33 Avatar asked Mar 24 '11 06:03

djibouti33


People also ask

What is NSNumber in C++?

An object wrapper for primitive scalar numeric values. NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned char, short int, int, long int, long long int, float, or double or as a BOOL.

How does an NSNumber object get converted to a different type?

An NSNumber object initialized with a value of a particular type accessing the converted value of a different kind of type, such as unsigned int and float, will convert its stored value to that converted type in the following ways: As with any class cluster, subclasses of NSNumber must override the primitive methods of its superclass, NSValue.

What is the use of compare method in NSNumber?

It also defines a compare (_:) method to determine the ordering of two NSNumber objects. NSNumber is “toll-free bridged” with its Core Foundation counterparts: CFNumber for integer and floating point values, and CFBoolean for Boolean values.

What is the use of return in NSNumber in Java?

Returns an NSNumber object initialized to contain value, treated as a double. Returns an NSNumber object initialized to contain a given value, treated as a float. Returns an NSNumber object initialized to contain a given value, treated as a signed int.


2 Answers

Do not use == to compare NSNumbers. Most of the time you'll be comparing two distinct objects, so the comparison won't evaluate to true. If you look at your if condition, notice that you're particularly comparing your property to a brand new NSNumber object.

Since NSInteger is a Cocoa wrapper for certain value types, comparing NSIntegers with == works fine.

The implementation of isEqualToNumber: probably takes the wrapped value types and compares them too.

like image 169
BoltClock Avatar answered Sep 30 '22 05:09

BoltClock


As you said, both solutions are working...

I would prefer the first one, as it appears more readable, IMHO... It may also be more performant, as you are comparing integers, after having converted a NSNumber to an int.

In the second one, you convert an int to an object, then you compare the two objects... So that's a second method call, which you don't have in the first case...

Hope this helps... : )

like image 31
Macmade Avatar answered Sep 30 '22 07:09

Macmade