• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Why can't I use any object as a key in a dictionary?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why can't I use any object as a key in a dictionary?


  • Subject: Re: Why can't I use any object as a key in a dictionary?
  • From: Roland King <email@hidden>
  • Date: Mon, 01 Feb 2010 16:26:53 +0800

Tollfree Bridging is a little more complicated than that. They may end up being the same object under the covers, but even if they are, the NSDictionary version doesn't come with the range of options that the CFDictionary does. Just make a CFDictionary(), the default for it is to retain keys (and values) so it's actually really, really easy; I use them all over the place for stuff like this.

If a have a non-NSDictionary compatible CFDictionary() like that by the way I use toll free bridging for reading values from it, but I don't use it for setting them, it doesn't seem to work.

By the way, CALayer is a KVC compliant class so you can in fact just store a reference to an arbitrary object in it with

[ layer setValue:value forKey:@"KeyForObjectAssociatedWithLayer" ];

and save yourself a whole world of pain.

Richard Penwell wrote:
I thought NSDictionary and CFDictionary were the same data object, that whole toll free bridging...

An ugly hack would be to cast the pointer to a numeric type, and encode that in a NSNumber... but I would feel very very ashamed of doing so.
On Feb 1, 2010, at 3:10 AM, Roland King wrote:



Because NSDictionary requires keys to be copyable because it copies them (it's in the documentation). Use a CFDictionary() instead, you can set it up to retain the keys and do what you want.

Rick Mann wrote:

I'd like to use a CALayer object as a key in a dictionary. The reason is that when my app detects a hit in a layer, I need to quickly determine which object I've associated with it. Since I can't store a reference to an arbitrary object in the CALayer, a dictionary seems to be the most expedient way to do that.
Unfortunately, I can't seem to add my layer as the key (it fails with "-[CALayer copyWithZone:]: unrecognized selector sent to instance 0x50132a0"). It's really pretty handy to be able to use any object as a key, why is this not the case in Obj-C?
TIA,
Rick
_______________________________________________
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

_______________________________________________

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


_______________________________________________

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


  • Follow-Ups:
    • Re: Why can't I use any object as a key in a dictionary?
      • From: Charles Jenkins <email@hidden>
    • Re: Why can't I use any object as a key in a dictionary?
      • From: Rick Mann <email@hidden>
References: 
 >Why can't I use any object as a key in a dictionary? (From: Rick Mann <email@hidden>)
 >Re: Why can't I use any object as a key in a dictionary? (From: Roland King <email@hidden>)

  • Prev by Date: Re: Crash in NSControl's sizeToFit
  • Next by Date: Re: Why can't I use any object as a key in a dictionary?
  • Previous by thread: Re: Why can't I use any object as a key in a dictionary?
  • Next by thread: Re: Why can't I use any object as a key in a dictionary?
  • Index(es):
    • Date
    • Thread