Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.objective-c > #223
| From | Don Bruder <dakidd@sonic.net> |
|---|---|
| Newsgroups | comp.lang.objective-c |
| Subject | Re: A question on designated initializers |
| Date | 2015-11-22 19:15 -0800 |
| Organization | Chaotic Creations Unlimited |
| Message-ID | <n2u08d$7vs$1@dont-email.me> (permalink) |
| References | <n2tcmc$t64$1@news.albasani.net> |
In article <n2tcmc$t64$1@news.albasani.net>,
Jon Rossen <jonr17@comcast.net> wrote:
> I have a rather beginner's basic type of question on designated
> initializers. The book that I'm using uses an extremely simple (and
> trivial) example (which is usually the case) which leaves me with about
> as many questions as answers.
>
> Here's the simple example from the book using a Rectangle class which is
> a child class of NSObject.
>
> Here's some of the pertinent info from Rectangle.h; I won't bother
> including stuff that's not directly related to the example:
>
> Rectangle.h
>
> @property int width, height;
>
> -(void) setWidth: (int) w andHeight: (int) h;
> -(instancetype) initWithWidth: (int) w andHeight: (int) h;
> -(instancetype) init;
>
>
> Ok, here are just the new, custom init methods from Rectangle.m
>
> Rectangle.m
>
> //Designated initializer
> -(instancetype) initWithWidth: (int) w andHeight: (int) h;
> {
> self = [super init];
> if (self)
> [self setWidth:w andHeight:h];
>
> return self;
> }
>
> //The override of super's designated initializer
> -(instancetype) init
> {
> return [self initWithWidth:0 andHeight:0];
> }
>
>
> Ok, I get what's going on here: You create a new designated initializer
> which handles a lot of the ivars that you feel are important. You have
> to include a call super's designated intializer and assign it to self,
> then the 'if' test, then your initialization code. You then override
> super's designated initializer (in this case it's NSObject's init) and
> return a call to your designated initializer. I get how the hierarchy
> is supposed to work.
>
> But how is this helpful? In my mind it's not helpful for a few
> different reasons. Reason 1: In the override to init you've hardwired
> the width and height to be 0. That's what NSObject's init would do, so
> it's not doing anything new or better. Reason 2: Even if you put
> different values in there say, 1 and 2 or whatever values you thought
> you might need a newly created Rectangle object to have, they are still
> hardwired values, buried inside the newly created init method. How is
> this helpful at all? You may want to have a bunch of rectangles of all
> different dimensions. At that point you'd have to change their
> dimensions with the proper accessor method. If so, why even both with
> this newly created init when you can just use NSObject's init and use
> the accessor method to tweak the height and width you want each
> Rectangle object to be?
>
> Am I correct that the trivial nature of this example sort of gets in the
> way of truly understanding the benefit or need of all of this?
> thanks,
> jonR
In a nutshell, the "designated initializer" is *THE* initializer for a
class that handles *ALL* possibilities for initialization that are
available - Everything else is convenience methods.
Ferinstance:
Assume you've got a class with 5 instance variables, A, B, C, D, and E
Your "designated initializer" should then look something like this:
-(MyClass *)initWithA:(id)a andB:(id)b andC:(id)c andD:(id)d andE:(id)e
{
// I'm ignoring the various checking and calls to super's init for
// brevity, since you seem to understand the chaining mechanism.
// Now stuff the a,b,c,d, and e instance variables with the appropriate
// values that were passed in - either by direct assignment, or via
// accessor methods, as you please - then return self.
}
Now, assuming there's some reason you only need to init an instance
with, ferinstance, a, while all the other instance vars can be left at
default values, you could write:
-(MyClass *)initWithJustA:(id)a
{ // (beware line-wrapping for post...)
return [InitWithA:a andB:defaultBValue andC:defaultCValue
andD:defaultDValue andE:defaultEValue];
}
Likewise, if you need to init A and B, but C, D, and E can be left at
default, you'd write:
-(MyClass *)initWithA:(id)a andB:(id)b
{ // (beware line-wrapping for post...)
return [initWithA:a andB:b andC:defaultCValue andD:defaultDValue
andE:defaultEValue];
}
and so on for each combination of initializations that make sense for
your code.
And finally, for an instance of the class that can be left with all of
the vars at default values, you could write:
-(MyClass *)init
{
return [InitWithA:defaultAValue andB:defaultBValue andC:defaultCValue
andD:defaultDValue andE:defaultEValue];
}
Notice how the "plain" init calls the designated initializer with
default values for each and every instance variable?
Further note that in the "not designated" initializers, you *DON'T* do
the [super init] checks and calls - You've written the "designated"
initializer so that it handles doing that. So long as you call the
designated initializer to do the actual initialization, that stuff gets
handled "automagically"
The whole point of the "designated initializer" is that an instance of a
class - any class - is expected to be "ready to use" immediately after
being initialized. Using a designated initializer makes certain that any
setup an instance needs done gets done, with nothing being forgotten.
--
Security provided by Mssrs Smith and/or Wesson. Brought to you by the letter Q
Back to comp.lang.objective-c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 13:39 -0800
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 14:48 -0800
Re: A question on designated initializers Louis Wu <louiswu@ringworld.net> - 2015-11-22 16:26 -0800
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 17:24 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-23 02:39 +0100
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-23 01:18 +0100
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 16:49 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-23 02:02 +0100
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-27 17:19 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-28 13:22 +0100
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-28 17:37 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-29 03:45 +0100
Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-22 19:15 -0800
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-23 18:44 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-24 04:58 +0100
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-24 16:42 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 02:00 +0100
Re: A question on designated initializers Greg Parker <gparker@apple.com> - 2015-11-25 01:25 -0800
Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-24 07:38 -0800
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-24 15:54 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 01:49 +0100
Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-24 18:51 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 04:41 +0100
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-25 00:25 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 14:16 +0100
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-25 17:30 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-26 03:03 +0100
Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-25 09:08 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 18:41 +0100
Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-12-02 14:37 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-12-03 00:58 +0100
Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-24 18:29 -0800
csiph-web