Re: NSNumber compare: Giving Unexpected Results
Re: NSNumber compare: Giving Unexpected Results
- Subject: Re: NSNumber compare: Giving Unexpected Results
- From: Quincey Morris <email@hidden>
- Date: Tue, 7 Sep 2010 20:43:11 -0700
On Sep 7, 2010, at 17:58, Chris Tracewell wrote:
> -(BOOL)isLessThanZero
> {
> NSNumber *theNumOne = [NSNumber numberWithInteger:0];
> NSNumber *theNumTwo = self;
>
> NSLog(@"theNumOne = %@",theNumOne);
> NSLog(@"theNumTwo = %@",theNumTwo);
> NSLog(@"NSOrderedSame = %d, NSOrderedAscending = %d, NSOrderedDescending = %d",NSOrderedSame,NSOrderedAscending,NSOrderedDescending);
> NSLog(@"SHOULD SAME = %d",[theNumTwo compare:theNumTwo]);
> NSLog(@"SHOULD ASCD = %d",[theNumOne compare:theNumTwo]);
> NSLog(@"SHOULD DESD = %d",[theNumTwo compare:theNumOne]);
>
> if ([theNumOne compare:theNumTwo] == NSOrderedDescending)
> {
> return YES;
> }
>
> return NO;
> }
The message in the 'if' ([theNumOne compare:theNumTwo]) is an invocation of [NSNumber compare:], not [NSDecimalNumber compare:]. It's not absolutely clear what is supposed to happen when the compare parameter is an instance of NSDecimalNumber. Yes, NSDecimalNumber is a subclass of NSNumber, but these are abstract classes. The actual objects are (typically) concrete subclasses of the abstract classes, such as NSCFNumber. It's possible that this muddies the waters enough that the mixed comparison doesn't work as you would expect.
In any case, why not simply do the comparison between 'self' and '[NSDecimalNumber zero]'?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden