For equality checks you can use isEqualToNumber , which checks if either id or content is equal (with the latter using compare ):
if ([a isEqualToNumber:b])
I donβt know why they also did not implement the convenience methods isGreaterThanNumber and isLessThanNumber (and possibly >= and <= ), since the compare method below looks a bit awkward.
To check for inequality, simply use compare directly (you can also do this for equality, as can be seen from the first below):
if ([a compare:b] == NSOrderedSame)
Details can be found on the NSNumber class documentation page .
Keep in mind that nothing prevents you from creating your own helper function, which, for example, would allow you to use the code:
if (nsnComp1 (a, ">=", b)) ... // returns true/false (yes/no)
or
if (nsnComp2 (a, b) >= 0) ...
although it is less than Objective-C and more than C :-) It depends on whether your definition of elegance is mainly related to efficiency or readability. Is it possible that your intValue option is preferable - this is a decision that you will need to make yourself.
source share