• 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: NSDictionary trouble
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDictionary trouble


  • Subject: Re: NSDictionary trouble
  • From: Jens Alfke <email@hidden>
  • Date: Tue, 19 Jan 2010 10:24:21 -0800


On Jan 19, 2010, at 8:53 AM, Shawn Rutledge wrote:

I wish NSDictionary (NSMutableDictionary actually) could handle
arbitrary mappings of one type of object to another, without copying
the keys.

It can. If you have a custom class you want to be able to use as a dictionary key without copying, then make it implement the NSCopying protocol and add this method:
- (id) copyWithZone: (NSZone*)zone {
return [self retain];
}
This enables -copy but turns it into a no-op that returns the original object. (This is perfectly kosher. Most immutable Foundation objects do the same thing, i.e. copying an immutable NSString returns the same object.)


Of course you also have to implement -hash and -isEqual: to make your class play nicely with dictionaries. Make sure these have immutable semantics: if the values returned by these can ever change, you'll screw up any dictionary or set the object is in.

I didn't try CFDictionary yet; is that appropriate for an iPhone app?

Yes, you can use CF. Remember that a CFXXXRef is the same as an NSXXX*, you can just typecast between them. If you create a CFDictionary you'll have to define your own callbacks, and make the copy callback just retain the object.


But try NSMapTable first. It's sort of halfway between the two — it's an Objective-C class but it has greater flexibility in what it can store and how it stores it.

—Jens_______________________________________________

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: NSDictionary trouble
      • From: Sixten Otto <email@hidden>
    • Re: NSDictionary trouble
      • From: Shawn Rutledge <email@hidden>
References: 
 >NSDictionary trouble (From: Shawn Rutledge <email@hidden>)

  • Prev by Date: Re: Blocking for input during a loop? (Yahoo mail destroyed my indentation)
  • Next by Date: Re: Blocking for input during a loop?
  • Previous by thread: Re: NSDictionary trouble
  • Next by thread: Re: NSDictionary trouble
  • Index(es):
    • Date
    • Thread