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


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

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-26 03:03 +0100
Organization Informatimago
Message-ID <87r3jdtt2p.fsf@kuiper.lan.informatimago.com> (permalink)
References (6 earlier) <n337jb$6mt$2@dont-email.me> <87d1uyvj8a.fsf@kuiper.lan.informatimago.com> <n33r9o$27f$1@news.albasani.net> <878u5muslu.fsf@kuiper.lan.informatimago.com> <n35nc1$48e$1@news.albasani.net>

Show all headers | View raw


Jon Rossen <jonr17@comcast.net> writes:

>> So you can have two methods:
>>
>>    -[MyClass designatedInitializer:]
>>    -[MySubClass designatedInitializer:]
>
> Ok, got it.  Just a nitpick...these are instance methods, not class
> methods so technically shouldn't this example be something like:
> [myClassObject designatedInitializer]
> [mySubclassObject designatedInitializer]


No. The point is that both methods can be called for the same object!

    -[MyClass designatedInitializer:]
    -[MySubClass designatedInitializer:]

For example:

    @implementation MySubClass
    -(id)init {
        // you can write:
        self=[self  designatedInitializer:42];
        // or you can write:
        self=[super designatedInitializer:42];
        return self;
    }
    @end

If you send the message to self, then it's the method:

    -[MySubClass designatedInitializer:]

that is called, and if you send the message to super, then
it's the method:

    -[MyClass designatedInitializer:]

that is called.


> ...or does the '-' you have as a prefix serve to state this and the
> examples (names used) are supposed to be taken abstractly?

Yes, the notation -[MyClass designatedInitializer:]
or:               +[NSObject alloc]
is not source code.  They are the name of actual methods.



>> and when you send a designatedInitializer: message:
>>
>>     [self designatedInitializer:42]
>>
>> one or the other method may be called first, depending on the class of
>> self.
>
> The version that is called first is the version that is in self's
> class. Please tell me that what I have said is correct, or else I'm
> going to kill myself after I destroy my computer.  :-)

If the class of self has such an instance method, yes.

If self is an instance of a sub sub class that doesn't define a method
for the selector -designatedInitializer: then it will be the method in
the next superclass, ie. MySubClass.


>> The class of self is not necessarily the class of the method being
>> executed!
>
> This statement seems at odds at the one right above that I commented
> on. What are you saying here?  You seem to be saying that the class of
> self may not be the class of the method being executed.  

Yes, this is what I'm saying.

> But I think I
> know why you are saying this.  You seem to be implying something here
> but you are not explicitly giving a specific scenario.

Indeed.  You need to imagine new examples.

Again, I assume that you wrote code, and that you inserted printing of
the class of the instance in the various methods.

Add lines such as:

    @implementation MyClass
    -… {
        …
        NSLog(@"in method %@ of class %@, class of self = %@",  
              NSStringFromSelector(_cmd),
              @"MyClass",
              [self className]);
       …
    }
    @end


    @implementation MySubClass
    -… {
        …
        NSLog(@"in method %@ of class %@, class of self = %@",  
              NSStringFromSelector(_cmd),
              @"MySubClass", // *** <- in subclass
              [self className]);
       …
    }
    @end

in each method and send a message and see


> Is this statement related to the fact that the search for the method
> executed first could be in *super's* class?  

Nope. The recipient is self, so the search starts in self's class.


> This is what you touched on a few comments above.  In other words in
> that scenario (if I'm understanding that correctly), a different
> method than the one in self's class would be executed.  Is this what
> you are saying here?

No. I'm saying that self could be an instance of a different class than
the class of the method where self is used.  That is, it could be an
instance of a subclass, or even possibly of an entirely different class.


This is why in the initializer you assign self.  The superclass
initializer may actually unallocate self, and allocate a new instance of
a completely different class (as long as that instances of this
different class can receive the same set of messages).

When you have class clusters, you will have normally a subclass so it
won't be "strange".  But there are other tricks, such as having proxy
objects.  Then the class of the object is not even a subclass, but an
entirely different class.  But you can ignore this for the moment, since
the proxy object will respond to the same messages as a normal object.

-- 
__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