Re: -[NSSet containsObject:] returns NO when it should return YES
Re: -[NSSet containsObject:] returns NO when it should return YES
- Subject: Re: -[NSSet containsObject:] returns NO when it should return YES
- From: Ken Thomases <email@hidden>
- Date: Sat, 19 Feb 2011 21:20:35 -0600
On Feb 19, 2011, at 9:01 PM, Michael Crawford wrote:
> On Feb 19, 2011, at 9:31 PM, Ken Thomases wrote:
>
>> On Feb 19, 2011, at 4:17 PM, Michael Crawford wrote:
>>
>>> I also assumed that whether or not the value is signed or unsigned; negative or positive makes no difference as long as calling the same accessor method on both NSNumber instances returns the same result.
>>
>> I think this is wrong. I can't imagine why you assumed that some future method call would affect whether two NSNumbers are equal. They are equal if they contain the same value. You must realize that, for example, -boolValue would map a great many different NSNumbers to the same return value, but that doesn't imply that the NSNumbers are equal.
>
> I assumed this is the case because regardless of whether or not you call -boolValue or -longLongValue, -hash returns the same result.
So? -hash being the same does not imply equality. Equality implies that the hash must be the same, which is not the same thing.
> I've already shown that they do indeed contain the same value, when -longLongValue is called on both items. See the dbg output.
And my point is exactly that this doesn't imply equality of the NSNumber objects. Why would you think it does? That's exactly like saying that calling -boolValue on both items gave the same result so therefore they must be equal.
Notice that when the description is printed, you got 16698445052422911484 from [mediaItem persistentIDProperty] while the elements of the set show -1748299021286641066, -1748299021286641065, and -1748299021286640132. That's an indication that the numbers are not the same "under the hood". At the very least, one is positive while the others are negative.
You are essentially asking NSNumber whether the numbers 16698445052422911484 and -1748299021286640132 are equal, and it's giving you the fairly obvious answer that, no, they are different numbers. Yes, when cast to long long, they both end up as the same long long value, but that's a transformation that throws away information about the original numbers. It doesn't mean the original numbers are equal.
Regards,
Ken
_______________________________________________
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