• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSDictionary trouble
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDictionary trouble


  • Subject: Re: NSDictionary trouble
  • From: mmalc Crawford <email@hidden>
  • Date: Tue, 19 Jan 2010 21:15:50 -0800

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@hidden)

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:

This email sent to email@hidden

References: 
 >NSDictionary trouble (From: Shawn Rutledge <email@hidden>)
 >Re: NSDictionary trouble (From: Jens Alfke <email@hidden>)
 >Re: NSDictionary trouble (From: Shawn Rutledge <email@hidden>)
 >Re: NSDictionary trouble (From: Graham Cox <email@hidden>)
 >Re: NSDictionary trouble (From: Shawn Rutledge <email@hidden>)
 >Re: NSDictionary trouble (From: Jens Alfke <email@hidden>)
 >RE: NSDictionary trouble (From: Jeff Laing <email@hidden>)
 >Re: NSDictionary trouble (From: mmalc Crawford <email@hidden>)
 >RE: NSDictionary trouble (From: Jeff Laing <email@hidden>)

  • Prev by Date: Re: NSDictionary trouble
  • Next by Date: OS X equivalent of UIImage stretchableImageWithLeftCapWidth:topCapHeight: method
  • Previous by thread: Re: NSDictionary trouble
  • Next by thread: Re: NSDictionary trouble
  • Index(es):
    • Date
    • Thread