Comparison of NSNumber iOS SDK 4.2 vs 5 in Objective-C?

I found an error in my application: the logic was that I was comparing two NSNumbers using "==" and I believe that it worked. But it no longer isEqualToNumber to iOS sdk 5, so I need to use isEqualToNumber .

Can anyone with iOS sdk 4.2 try to run the following code and give me the result. I tried to revert to an earlier Xcode to test it myself, but I could not do it.

 NSNumber *num1 = [NSNumber numberWithInt:100]; NSNumber *num2 = [NSNumber numberWithInt:100]; if (num1 == num2) { NSLog(@"== YES"); } else { NSLog(@"== NO"); } 
0
source share
1 answer

Here isEqualToNumber is the right solution. The fact that he worked for the job is solely a detail of the implementation of how the numbers were cached by the system domestically. You should never (read, probably not what you really want), compare objects with == . == on objects will compare their memory address, not if they are actually equal.

+6
source

All Articles