Compare NSNumber vs NSInteger

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

Now that I have a solution, I was hoping to get some clarity.

I am comparing an attribute from Core Data (Integer 16 / NSNumber) with Integer (ABPropertyID and ABMultiValueIdentifier).

The error in this comparison, and, oddly enough, showed up only after I killed the application (from the background tray), opened it again and skipped the same process that included the comparison. Anyway...

This is what stopped working after reboot:

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

And these are two solutions that still work perfectly:

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

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

To me they all look the same. I always either convert NSNumber to integerValue, or convert an integer to NSNumber.

Any ideas?

+5
3

== NSNumber s. , true. if, , NSNumber .

NSInteger - Cocoa , NSInteger == .

isEqualToNumber:, , .

+11

, ...

, , ... , NSNumber int.

, int , ... , ...

, ...:)

+2

netWorkingButtonsIndexesis an array that contains objects and LinkedInis a number with a data type int.

[[netWorkingButtonsIndexes objectAtIndex:buttonIndex] isEqual:[NSNumber numberWithInteger:LinkedIn]] 

Using the method isEqual, we can compare objects with any rtype data.

+1
source

All Articles