On 23.07.2012, at 10:36, Mike Abdullah wrote:

> I think all this begs the questions:
> 
> - Why do you want to get these objects stored in arrays rather than a 
> dictionary?
> - Why C arrays, rather than NSArray?

Well, I want to create a new dictionary from copies of elements from another 
dictionary. I cannot use simply copy or mutableCopy since I want to implement a 
"deep" copy.

There is 

1)  + (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys

and

2)  + (id)dictionaryWithObjects:(const id[])objects forKeys:(const id < 
NSCopying >[])keys count:(NSUInteger)count

for creation.



I cannot use 1), e.g.

NSArray* keys = [dictionary allKeys];
NSArray* values = [dictionary allValues];

since the order in values is not defined.


Using 
- (void)getObjects:(id __unsafe_unretained [])objects andKeys:(id 
__unsafe_unretained [])keys
however, returns objects in order according their association.

Using the plain C-arry API and manual release/retain would be fine, but it 
won't work with ARC.


Sure, I could use fast enumeration, and a separate NSArray for keys and values 
to accomplish what I want. But I was also hoping, the first approach is 
possibly faster. Using fast enumeration appears quite suboptimal for this task:

NSMutableArray* keys = [NSMutableArray array];
NSMutableArray* values = [NSMutableArray array];
for (id key = dict] {
        id value = [dict objectForKey:key];
        // simplified!
        [keys addObject:[key deepCopy]];
        [values addObject:[value deepCopy]];
}
NSDictionary* result = [NSDictionary dictionaryWithObjects:values forKeys:keys];


By the way: why don't we have:
for (id<NSPair> p = dict] {
        [values addObject:[p.first deepCopy]];
        [keys addObject:[p.second deepCopy]];
}
(which might probably be faster)



Andreas
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to