On Jan 19, 2010, at 8:47 pm, Jeff Laing wrote:
> I wrote:
>>> No, you should just *retain* the result of dictionaryWithCapacity.
>
> mmalc wrote:
>> No, you shouldn't.
> With all due respect, why not?
>
Because it's difficult to imagine a common situation in which your advice will
be valid, for example:
NSMutableDictionary *dict = [NSMutableDictionary
dictionaryWithCapacity:x];
// Add to the dictionary.
myInstanceVariable = [dict retain];
Wrong, because you're setting an instance variable directly instead of using an
accessor.
NSMutableDictionary *dict = [[NSMutableDictionary
dictionaryWithCapacity:x] retain];
// Use the dictionary.
[dict release];
Retaining the dictionary is pointless.
> Apple gives us all those convenience methods but you say we shouldn't use
> them?
>
Please don't misrepresent what I wrote. I didn't say that. There are situations
in which the convenience API is useful and not discouraged. In particular, for
example, if you're creating a dictionary as a return value from a method, then
it's perfectly reasonable to use a convenience method:
- (NSDictionary *)dictionaryRepresentation {
NSMutableDictionary *dict = [NSMutableDictionary
dictionaryWithCapacity:5];
// Use the dictionary.
return dict;
}
(Also if you're using garbage collection, any of the convenience methods are
perfectly reasonable.)
In most other cases, however, Jens and Ken have already addressed the principal
issue: performance. You don't want to needlessly prolong the lifetime of
objects.
>> If you want to assign the dictionary to an instance variable, you
>> should use an accessor method. In this case, it's still better to use
>> alloc/init then release the dictionary when you've finished with it.
> That's seems a really aberrant case, where you are creating an empty
> dictionary with a nominated capacity. I can't visualize a case where you
> would do such a thing, outside of the initialiser.
>
This pattern is shown commonly in iPhone sample code:
NSMutableDictionary *aDictionary = [[NSMutableDictionary alloc]
initWithCapacity:x]
// Add to the dictionary.
self.dictionary = aDictionary;
[aDictionary release];
> There are cases like the NSFileManager where you need to pass in a dictionary
> of options that describe the directory being created (for example) - should I
> really be creating temporary variables for those, or using the convenience
> methods (dictionaryWithObject:forKey:)?
>
The same applies as above.
Following best practices, you should use alloc/init+release. How important this
is to adhere to depends on how performance-sensitive your code and platform
are...
To reiterate Jens' succinct formula:
Convenience ≠ performance.
mmalc
_______________________________________________
Cocoa-dev mailing list ([email protected])
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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]