Re: Objects as keys NSMutableDictionary
Re: Objects as keys NSMutableDictionary
- Subject: Re: Objects as keys NSMutableDictionary
- From: Agent M <email@hidden>
- Date: Mon, 10 Jul 2006 20:41:26 -0400
Another option is to pass string keys around using some two-way
conversion method. For example, the object's address concatenated with
its creation time is a good one. Using -description could also work.
-M
On Jul 9, 2006, at 9:51 AM, Adam R. Maxwell wrote:
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.
¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬
AgentM
email@hidden
¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬
_______________________________________________
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