• 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: confused about allocating and releasing objects
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: confused about allocating and releasing objects


  • Subject: Re: confused about allocating and releasing objects
  • From: Devon Ferns <email@hidden>
  • Date: Wed, 27 Aug 2008 09:40:47 -0400

If you had access to the NSDictionary source code you'd find that the NSDictionary's +dictionaryWithContentsOfFile:(NSString*)fileName would probably look something like this:

+(NSDictionary*) dictionaryWithContentsOfFile:(NSString*)fileName
{
	return [[[NSDictionary alloc] initWithContentsOfFile] autorelease];

}

As you can see it's not really doing anything all that special. It's really just saving you some typing and having to release it yourself if it's for a temporary variable.

Devon

Memo Akten wrote:
HI All, i'm a bit confused about the 2 scenarios:

NSDictionary *myData1 = [NSDictionary dictionaryWithContentsOfFile:@"mydata.plist"]; // this one I don't need to release when I'm done?
NSDictionary *myData2 = [[NSDictionary alloc] initWithContentsOfFile:@"mydata.plist"]; // this one I do need to relase when I'm done?


There seems to be a lot of situations in Cocoa where this happens so I guess this is a generic question for all cases when an object is created via means of a static method vs using alloc:initWith... (e.g. NSArray, NSString, NSNumber etc. are just a few I've seen).

Whats the difference between the two methods? (I know the second one creates a blank dictionary first, and then loads the file - but I mean which one is better for what purpose? - why would I choose one over the other). And am I correct in assuming if I use the first method i do not need to release the variable when I'm done? but do need to release the variable in the second instance?

Cheers,

Memo.
_______________________________________________

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
_______________________________________________

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: 
 >confused about allocating and releasing objects (From: Memo Akten <email@hidden>)

  • Prev by Date: Re: 2 NSRuleEditor questions?
  • Next by Date: Re: [NSWindow] Is it a bug in Cocoa or a task for the developer?
  • Previous by thread: Re: confused about allocating and releasing objects
  • Next by thread: [NSWindow] Is it a bug in Cocoa or a task for the developer?
  • Index(es):
    • Date
    • Thread