Re: NSNumber : method to return pointer to represented value
Re: NSNumber : method to return pointer to represented value
- Subject: Re: NSNumber : method to return pointer to represented value
- From: Graham Cox <email@hidden>
- Date: Sun, 23 Feb 2014 21:05:02 +1100
On 23 Feb 2014, at 8:30 pm, email@hidden wrote:
> This dict, when instantiated, will contain 1 key value pair.
> NSDictionary *dict = @{ @((int)1): @"val 1", @((float)1): @"val 2" };
>
> However, this C# Dictionary<TKey, TValue> unit test object will contain two,
> ObjectObjectDictionary = new Dictionary<object, object> {
> { 1, 8 },
> { 1F, "Dubrovnik.UnitTests" },
> };
>
> When I convert ObjectObjectDictionary to an NSDictionary it will loose a key value pair (and that very fact may ultimately push me down a custom class route, but my insight into this isn’t good enough just yet).
> However if I query the underlying object with @((int)1) I will receive 8.
> if I query the underlying object with @((float)1) I will receive "Dubrovnik.UnitTests" .
Sorry for the multiple replies, I really should gather my thoughts first and put it all in one reply.
The reason you lose a key/value pair in NSDictionary is that the keys 1(int) and 1(float) will compare equal. NSDictionary uses -isEqual to test its keys. Even if it ends up with two different NSNumber objects for the keys, they'll compare equal. However, the fact it works in the C# code suggests that they are in fact two different objects (and that class probably isn't testing for equality by calling a method, perhaps looking directly at pointer values). But even that is likely to fail as NSNumber is free to make the two cases into one, as its documentation explicitly states.
You might find it happens to work today but it is not contracted to do so - 1(int) and 1(float) can be represented by the same NSNumber object perfectly legally.
--Graham
_______________________________________________
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