• 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: question about -init methods
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: question about -init methods


  • Subject: Re: question about -init methods
  • From: Bill Bumgarner <email@hidden>
  • Date: Fri, 7 Jul 2006 10:38:37 -0700

On Jul 7, 2006, at 10:25 AM, Matt Neuburg wrote:
On Fri, 7 Jul 2006 18:54:59 +0530, Vinay Prabhu
<email@hidden> said:
It can be done, provided the alloc should not be called again.

Lots of things *can* be done. The question is, *should* it?

(Example: you *can* write a dealloc method without calling super dealloc.
But don't!)


I think what I'd like to hear the OP focus on is *why* he thinks he might
need to do this. Since an instance "re-initialized back to its initial
state" would be effectively the same as a completely new instance, why not
use a completely new instance? What do you think "re- initialization" would
get you? m.

Calling the designated initializer(s) more than once on any given instance is generally frowned upon. Don't do it.


For many Apple classes, it will cause crashes and exceptions. For example, class clusters typically munch upon the instance being initialized quite severely and the returned, initialized, object and subsequent attempts to re-initialize will fail:

>>> from Foundation import *
>>> x = NSArray.arrayWithObjects_(1,2,3,None)
>>> type(x)
<objective-c class NSCFArray at 0x616d80>
>>> x.initWithCapacity_(10)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: NSInvalidArgumentException - *** initialization method - initWithCapacity: cannot be sent to an abstract object of class NSCFArray: Create a concrete instance!
>>>


This is a pattern that should be preserved in classes you create. Once an instance is initialized, it should not be-reinitialized via the designated initializer.

If you want to create a class that can "reset" itself back to some initial state, then implement a -(void)reset method and call it from your designated initializer. Then, you can always -reset in the future to bring it back to the "initial state".

This pattern can have advantages if there is some significant chunk of otherwise relatively static data that is associated with the instance that does not have to change across the reset back to initial state. If that data is expensive to gather together, resetting back to an initial state may be a performance boon.

Otherwise, I agree with Matt -- a default behavior of tossing the instance and creating a new one is generally preferable.

b.bum
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: question about -init methods (From: Matt Neuburg <email@hidden>)

  • Prev by Date: Re: [Moderator] List guidelines and related resources
  • Next by Date: NSDocumentController overriding NSApp delegate?
  • Previous by thread: Re: question about -init methods
  • Next by thread: Re: question about -init methods
  • Index(es):
    • Date
    • Thread