Reputation: 1980
I was under the impression that to test the value of an NSNumber you may use isEqual:
However, when I test the value of an NSNumber with an integer value of 38 using the following code, I am getting false instead of true. Is there a subtlety to this that I am missing?
if ([self.adding isEqual:@38]) {
//they are equal
}
else {
//they are unequal
}
Of note, when I use if ([self.adding intValue]==38)
, I do get the expected result.
In the debugger the NSNumber shows as int(38)
.
Upvotes: 0
Views: 230
Reputation: 71088
Use -isEqualToNumber:
for a numeric equality test, which is itself tied to the -compare:
method's notion of equality.
(NSNumber
is implemented as a class cluster. The guts of the -isEqual:
method are likely looking at additional subtleties like whether the two instances are from the same actual type or whatever, which might be significant when dealing with storage in a collection but don't help you check for the 38. This is the same reason you should use NSString's -isEqualToString:
method for the equivalent comparison).
Upvotes: 1