• 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: Newbie Question (alloc, init, retain?)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie Question (alloc, init, retain?)


  • Subject: Re: Newbie Question (alloc, init, retain?)
  • From: Tom Sutcliffe <email@hidden>
  • Date: Wed, 28 May 2003 15:16:10 +0100

I've been reading the list for a long time and now I've started something
that has prompted a question.

I had an idea for a very simple app to use as a learning exercise. My app
is a one column table that the user types numbers into and the mean average
is calculated as the user types in new numbers.

My question is about memory management and object allocation and
initialization. When a number gets added, I add an NSNumber set to 0.0 to
an NSMutuableArray, something like this:

NSNumber *foo;
foo = [NSNumber numberWithFloat:0.0];
[Numbers insertObject:foo atIndex:[Numbers count]];
[theTableView reloadData];

Do I have to send [[foo alloc] init]? How do I know which classes need this
message?

No. The numberWithFloat method is shorthand for [[[NSNumber alloc] initWithFloat:0.0] autorelease] and what you have written is perfectly correct.

You only allocate and initialise objects you directly create yourself. The numberWithFloat method is responsible for doing that before it returns it to you. Any method that returns an object must have already alloc'ed and init'ed it!

This goes for any class and any method - either you call a method that returns an autoreleased object (in which case no need to init it yourself), or you alloc and init it directly (and then optionally autorelease/retain/whatever). In fact I'm not sure what would happen if you tried to init an object that was already initialised - it would depend on the object and you don't want to do this!

Also since I just add the object to the array, do I need to send [foo
retain]?

No the insertAtIndex method automatically sends a retain message for you (and for balance the removeObjectAtIndex method sends a release) so you don't need to worry about it.

Thanks for any help. The light bulb just hasn't gone on yet.

Hope this helps the lightbulb :)

Tom
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Newbie Question (alloc, init, retain?) (From: Rob Cohen <email@hidden>)

  • Prev by Date: Interface Builder and projects with variable number of windows
  • Next by Date: Re: Forwarding to mimic multiple inheritance
  • Previous by thread: Newbie Question (alloc, init, retain?)
  • Next by thread: RE: Newbie Question (alloc, init, retain?)
  • Index(es):
    • Date
    • Thread