• 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: Objects as keys NSMutableDictionary
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Objects as keys NSMutableDictionary


  • Subject: Re: Objects as keys NSMutableDictionary
  • From: "Adam R. Maxwell" <email@hidden>
  • Date: Sun, 9 Jul 2006 06:51:28 -0700


On Jul 9, 2006, at 06:11, Matthias Winkelmann wrote:

I'm having a little problem with NSMutableDictionary and would like to find out what "best practice" for this is.

Here's a contrived example: A class "exam" should store the result for each student. So we have 20 students and an NSNumber for each. I'd like to store them like this:

in init:
NSMutableDictionary *result = [NSMutableDictionary dictionary];


- (void)setResult:(NSNumber *)aResult forStudent:(Student *)aStudent { [results setObject:aResult forKey:aStudent]; }

Now firstly, NSDictionary likes NSStrings as keys for key-value coding. I don't depend on that, so I'm willing to ignore it. However, it also requires the keys to be immutable, so I'd have to copy the student object. I don't want to do that, since I want to later be able to go back and retrieve the result for that specific student.

You could implement -[Student copyWithZone:] to return [self retain], but that may have side effects you don't want.


Another solution would be to create a CFMutableDictionary with custom callbacks, where you can specify the dictionary's retain/release/ equality/hash behavior explicitly. A recent post to this list from Mike Ash notes that you have to use the CF functions to add values to the dictionary if you do this, but that's not too painful.

-- Adam
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Objects as keys NSMutableDictionary
      • From: Agent M <email@hidden>
References: 
 >Objects as keys NSMutableDictionary (From: Matthias Winkelmann <email@hidden>)

  • Prev by Date: Objects as keys NSMutableDictionary
  • Next by Date: Re: Objects as keys NSMutableDictionary
  • Previous by thread: Objects as keys NSMutableDictionary
  • Next by thread: Re: Objects as keys NSMutableDictionary
  • Index(es):
    • Date
    • Thread