Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.objective-c > #233

Re: A question on designated initializers

From "Pascal J. Bourguignon" <pjb@informatimago.com>
Newsgroups comp.lang.objective-c
Subject Re: A question on designated initializers
Date 2015-11-25 04:41 +0100
Organization Informatimago
Message-ID <87d1uyvj8a.fsf@kuiper.lan.informatimago.com> (permalink)
References (2 earlier) <n30iuo$c4b$1@news.albasani.net> <n32058$rkn$2@dont-email.me> <n32tc0$hik$1@news.albasani.net> <87oaeivr7a.fsf@kuiper.lan.informatimago.com> <n337jb$6mt$2@dont-email.me>

Show all headers | View raw


Don Bruder <dakidd@sonic.net> writes:

> In article <87oaeivr7a.fsf@kuiper.lan.informatimago.com>,
>  "Pascal J. Bourguignon" <pjb@informatimago.com> wrote:
>
>
>> I don't agree with Don's code.  
>> 
>> In general the subclass should call:
>>    [super designatedInitializer:argument]
>> and not [super init], in its -designatedInitializer: method.
>> 
>> Notably, when the argument is given as a parameter to the subclass
>> -designatedInitializer:!
>
> OK, now *I'M* confused... Lemme go look at the code...
> Now that I've gone and looked, I'm gonna update that to "I'm COMPLETELY 
> lost!" Please snatch in a copy of the code you're talking about and 
> annotate it to tell me specifically what it is you consider wrong with 
> it. I can't see anything (which is not to say it's impossible for me to 
> be wrong - only that if I am, I'm not seeing how or where based on what 
> you said), and I can't parse what you're saying up there in any way that 
> seems to make sense.
>
> Never mind snatching it in... I've got it here, and I still can't see 
> what might be wrong.
>
> ???
>
>> ---cut---
>> #import <Cocoa/Cocoa.h>
>> 
>> @interface MyClass : NSObject 
>> {
>>    // MyClass specific vars
>>    NSDictionary *myClassVars;
>> }
>> // And some method declarations
>> -(MyClass *)designatedInitializer:(id)anyNeededParams;
>> -(MyClass *)init;
>> // More stuff will likely live here in a useful program.
>> @end
>> ---cut---
>> 
>> And in your .m file:
>> ---cut---
>> #import "MyClass.h"
>> @implementation MyClass
>> 
>> -(MyClass *)designatedInitializer:(id)anyNeededParams
>> {
>>    self = [super init]; // Invoke NSObject's init
>>    
>>    // Do MyClass-specific init
>> 
>>    // and return what self has become.
>>    return self;
>> }
>> 
>> -(MyClass *)init
>> {
>>    // I'll assume defaultParams is set appropriately elsewhere
>>    return [self designatedInitializer:defaultParams];
>> }
>> 
>> // Any other methods MyClass needs follow
>> @end
>> ---cut---
>
>> ---cut---
>> #import "MyClass.h"
>> @interface MySublass : MyClass
>> {
>>    // MySublass specific vars
>>    NSDictionary *SubclassVar;
>> }
>> // And don't forget the method declarations
>> -(MyClass *)designatedInitializer:(id)anyNeededParams;
>> -(MyClass *)init;
>> 
>> @end
>> ---cut---
>> 
>> And the .m file
>> ---cut---
>> #import "MySubclass.h"
>> -(MySubclass *)designatedInitializer:(id)anyNeededParams
>> {
>>    self = [super init]; // Invokes MyClass' init

This.  The designated initializer of the super class (ie. the MyClass
class), is designatedInitializer: ; therefore this is the designated
initializer you must call from the subclass.
Since you call [super init], -[MyClass init] will send the
-designatedInitializer:  message to self, which will call 
-[MySubclass designatedInitializer:] and this will give an infinite
loop.



>>    // Do MySubclass-specific init work
>> 
>>    // and return what self has become.
>>    return self;
>> }
>> 
>> -(MySubclass *)init
>> {
>>    // I'll assume defaultMySubclassParams is set appropriately somewhere
>>    return [self designatedInitializer:defaultMySubclassParams];
>> }
>> 
>> // Any other methods MySubclass needs follow
>> 
>> @end
>> ---cut---

-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk

Back to comp.lang.objective-c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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