-[NSSet containsObject:] returns NO when it should return YES
-[NSSet containsObject:] returns NO when it should return YES
- Subject: -[NSSet containsObject:] returns NO when it should return YES
- From: Michael Crawford <email@hidden>
- Date: Sat, 19 Feb 2011 17:17:36 -0500
I'm trying to compare instances of NSNumber using NSSet. I have a set of numbers, which represent 64-bit persistent IDs for iTunes media-items. I access these values using the -[NSNumber longLongValue] method. I'm adding them to an instance of NSSet and then use that instance to determine whether or not I've seen the given item before.
The application is a black-list of songs and albums that want to avoid processing when scanning the iTunes library. I find that the comparison does not always work when it should and I am at a loss to explain why.
I assumed that the -[NSObject hash] method is used to determining equality and that to different NSNumber instances will hash out to the same value if they have the same 64-bit value stored inside.
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.
It looks like at least one, maybe both of these assumptions are wrong. Since I'm working with both Core Data and the MediaLibrary APIs on iOS, I'm a little wary of subclassing NSNumber in an attempt to fix the hash method.
The following gdb console output demonstrates an instance of a media-item persistent-ID property that, when queried gives a large unsigned number. When I ask again using the -longLongValue method, I get the answer I was expecting. When I look inside the set, you will see that the value -1748299021286640132 is in the set. However, when -containsObject is called, it returns NO.
(gdb) po mediaItem
<MPConcreteMediaItem: 0x6e752f0> 16698445052422911484
(gdb) po [mediaItem persistentID]
0x930bf5fc does not appear to point to a valid object.
(gdb) po [mediaItem persistentIDProperty]
16698445052422911484
(gdb) p (NSNumber*) [mediaItem persistentIDProperty]
$1 = (NSNumber *) 0x79681b0
(gdb) p (long long)[$1 longLongValue]
$2 = -1748299021286640132
(gdb) po noPlaySongs
{(
-1748299021286641066,
-1748299021286641065,
-1748299021286640132
)}
(gdb) continue
Anyone have previous experience with this problem? Can you shed some light on where my thinking and assumptions are wrong. I could write a sweet of tests to try and prove/disprove what I think is happening but I'm hoping to benefit from someone else's pre-existing research, experiments, or internal knowledge.
My Google incantation did not yield any results, either.
Thanks.
-Michael
_______________________________________________
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