• 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: modifying self in init
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: modifying self in init


  • Subject: Re: modifying self in init
  • From: Ondra Cada <email@hidden>
  • Date: Wed, 15 Sep 2004 13:26:07 +0200

Mike,

On 15.9.2004, at 8:08, Mike Bolton wrote:

@implementation A
- (id) initWithPath:(NSString *)path {
  if (self = [super init]) {

This is *NOT IMPORTANT*, but you may want to learn to use double parentheses around assignments used as boolean expressions: "if ((a=b))...", "while ((o=[en nextObject])) ...", etc. It would allow you to use the appropriate compiler warning to catch those cases when you just made typo and forget a '='.


     //read header of file
     if (fileType == 2) {
          [self release];
          self = [[B alloc] init];  //maybe I need a retain here?

No way. Alloc/init (and copy, mutableCopy, new) makes an object which does not need to be retained.


     }
     [self setFilename:path];
     ivar1 = blah;
     ivar2 = bleh;

It would be somewhat better to use accessors for ivars, too--see below.

  }
   return self;
}
@end

this seems to work, although I'm not sure if modifying
what self points to is a good idea.

It is. It's exactly how init behaviour is defined: you may modify self if there is reason to. Check archives, it was discussed quite in detail a year or two ago.


     [self setIvar1:blah];
     [self setIvar2:bleh];

This is always better. It makes the init independent on the actual implementation of ivar1, ivar2: a subclass may change their implementation. Note also that this, being the designated initializer, would be called for subclasses, if any.


(Of course, you may not indend this class for subclassing, ever; if so, you don't need to care so much :))

the problem with the second implementation is that I
need to create setter methods for each ivar I want to
assign values to (not a big deal, but the first
implementation was able to avoid this).

You may use KVC instead:

[self setValue:blah forKey:@"ivar1"];

The advantage here is that the KVC implementation would automatically use an accessor if any, and would just fill the ivar if there is no accessor.

Although a
solution to this would be to create another method
which is responsible for setting the default values of
the objects, although this is normally what the init
routine is responsible for.

Right, though the general design of

-(void)reset { ... }
-init {
  if ((self=[super init])) [self reset];
  return self;
}

often comes pretty handy, and is quite all right. If you feel it (or something similar) would help you, there's no reason to dodge it.

... I've searched the list
archives for similar problems, but I didn't find any
messages which addressed modifying the value of self.

Indeed?

http://www.cocoabuilder.com/archive/message/cocoa/2002/7/17/62744

and better even

http://www.cocoabuilder.com/archive/message/cocoa/2001/8/6/38569

and the following thread.
---
Ondra Čada
OCSoftware:     email@hidden               http://www.ocs.cz
private         email@hidden             http://www.ocs.cz/oc

_______________________________________________
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


  • Follow-Ups:
    • implementing dataWithContentsOfMemory as NSData category
      • From: Mike Bolton <email@hidden>
References: 
 >modifying self in init (From: Mike Bolton <email@hidden>)

  • Prev by Date: Re: Get my application to be in the dock automatically.
  • Next by Date: NSAttributedString size
  • Previous by thread: Re: modifying self in init
  • Next by thread: implementing dataWithContentsOfMemory as NSData category
  • Index(es):
    • Date
    • Thread