Re: multiple keys for object?
Re: multiple keys for object?
- Subject: Re: multiple keys for object?
- From: Ondra Cada <email@hidden>
- Date: Sat, 2 Apr 2005 11:36:24 +0200
Just to add something quite self-evident :)
On 2.4.2005, at 2:43, Justin Spahr-Summers wrote:
You don't need multiple dictionaries for that. A dictionary cannot
have two identical keys, but it can have two (or more) identical
objects.
Quite. And presumed you would ever need to store two or more objects
for one key, it's quite easy to store them into an array or a set.
Myself, I've found that task common enough to make me this category:
@implementation NSMutableDictionary (OCSExtensionsCategory)
-(void)addObjectToArray:o forKey:key {
NSMutableArray *ma=[self objectForKey:key];
if (!ma) [self setObject:ma=[NSMutableArray array] forKey:key];
[ma addObject:o];
}
-(void)addObjectToUniqueArray:o forKey:key {
NSMutableArray *ma=[self objectForKey:key];
if ([ma containsObject:o]) return;
if (!ma) [self setObject:ma=[NSMutableArray array] forKey:key];
[ma addObject:o];
}
-(void)addObjectToSet:o forKey:key {
NSMutableSet *ms=[self objectForKey:key];
if (!ms) [self setObject:ms=[NSMutableSet set] forKey:key];
[ms addObject:o];
}
-(void)addObjectToCSet:o forKey:key {
NSCountedSet *ms=[self objectForKey:key];
if (!ms) [self setObject:ms=[NSCountedSet set] forKey:key];
[ms addObject:o];
}
-(void)removeObject:o forKey:key keepEmpty:(BOOL)keep {
id container=[self objectForKey:key];
[container removeObject:o];
if (!keep && [container count]==0) [self removeObjectForKey:key];
}
@end
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
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