FROM : Adam R. Maxwell
DATE : Sun Jul 09 15:51:28 2006
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
DATE : Sun Jul 09 15:51:28 2006
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
| Related mails | Author | Date |
|---|---|---|
| Matthias Winkelman… | Jul 9, 15:11 | |
| Adam R. Maxwell | Jul 9, 15:51 | |
| James Bucanek | Jul 9, 16:17 | |
| wadeslists | Jul 9, 16:49 | |
| Rosyna | Jul 10, 07:16 | |
| Adam R. Maxwell | Jul 10, 15:33 | |
| Rosyna | Jul 10, 15:43 | |
| Adam R. Maxwell | Jul 10, 16:09 | |
| Rosyna | Jul 10, 16:19 | |
| Shawn Erickson | Jul 10, 18:00 | |
| Adam Maxwell | Jul 10, 18:05 | |
| Michael Ash | Jul 10, 19:32 | |
| Agent M | Jul 11, 02:41 |






Cocoa mail archive

