• 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: sending a message from an initializer method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sending a message from an initializer method


  • Subject: Re: sending a message from an initializer method
  • From: Andreas Grosam <email@hidden>
  • Date: Fri, 14 Jan 2011 10:53:50 +0100

On Jan 14, 2011, at 1:29 AM, Richard Somers wrote:

> I often will do something like this.
>
> - (id)init
> {
>     self = [super init];
>     if (self) {
>         [self prepare...];
>         [self prepare...];
>         [self prepare...];
>         // etc...
>     }
>     return self;
> }
>
> The methods 'prepare...' are all private methods. The preparation code for one of my classes is over 300 lines with five individual prepare methods. Breaking it up like this keeps it organized, understandable, and aids in debugging and refactoring. It works very well.

unless a subclass accidentally overrides -prepareX:   ...

The ubiquitousness of polymorphic methods and the lack of visibility rules in the runtime may make initializing of objects cumbersome, error prone and fragile.
So, just be careful.

If you don't want to have subclasses accidentally override your methods, you may want to prefix them:

@interface MyClass (Private)  {
    -(void) MyClass_init();
    -(void) MyClass_foo:(id x);
}

- (void) awakeFromNib {
    [self MyClass_init];
}
- (id) init {
    self = [super init];
    if (self) {
        [MyClass_init];
    }
    return self;
}

Though, there is no guarantee.


Regards
Andreas_______________________________________________

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: 
 >sending a message from an initializer method (From: Luc Van Bogaert <email@hidden>)
 >Re: sending a message from an initializer method (From: Richard Somers <email@hidden>)

  • Prev by Date: Overriding target and action of backBarButtonItem
  • Next by Date: Mutable to-many relationship not observable
  • Previous by thread: Re: sending a message from an initializer method
  • Next by thread: Re: sending a message from an initializer method
  • Index(es):
    • Date
    • Thread