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


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

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-23 02:39 +0100
Organization Informatimago
Message-ID <87a8q5wl3f.fsf@kuiper.lan.informatimago.com> (permalink)
References <n2tcmc$t64$1@news.albasani.net> <n2tgoe$59u$1@news.albasani.net> <louiswu-F7B886.16264522112015@news.giganews.com> <n2tpt4$3pn$1@news.albasani.net>

Show all headers | View raw


Jon Rossen <jonr17@comcast.net> writes:

>> You SHOULD use the designated initializer for creating objects of the
>> class because it was set up to ensure that the object is initialized in
>> a safe and consistent manner.
>>
>> However, since your new class may be initialized by some other means,
>> the basic form of the init should be overridden to make sure that the
>> designated initializer is used to create the object.
>
>
>
>> Suppose, for instance that you need to create Rectangle instances by
>> using only the class name using ...
>
>> NSObject *myRect = [[NSClassFromString(@"Rectangle") alloc] init];
>>
>> Ensuring that the init method (which you overrode) calls the designated
>> initializer will make sure that this behaviour "just works".
>
> Thanks for the response!
>
> I do not understand this example using NSClassFromString that you have
> provided.  In my original post, when I said this was a beginner's
> question, I sort of meant it. :-)  When using your example:
> NSObject *myRect = [[NSClassFromString(@"Rectangle") alloc] init]; an
> instance of the Rectangle class is not created.  

Yes it is.

   [NSClassFromString(@"Rectangle") alloc]

is the same as:

   [Rectangle alloc]

But we could have:

   NSString* className=[file readLine];
   NSObject* object=[[NSClassFromString(className) alloc] init];

and obtain an object of a class unknown at compilation time.


> Was there a typo? Did
> you mean to state: Rectangle *myRect....??

Rectangle being a subclass of NSObject, it's basically the same.

Objective-C is a dynamically typed programming language: there's a
run-time type associated with the value of the object, independent on
the C type associated to the variable.

For objects, you could as well always use the id type for all objects.

The only advantage of using a specific (super)class to define the type
of variables holding objects, is that it allows the compiler to issue
warning about the messages sent to the objects bound to that variable.



> Also, why is the importance of having overridden init specifically
> illustrated by this example using NSClassFromString?  

Again, the example is insufficient, since a literal class name was
given.  But the example shows that this class name can be determined at
run-time (it is little probable that the compiler would optimize out the
call to NSClassFromString).


> Again, I'm assuming that my lack of understanding of the simple
> example you have chosen is the reason why I am not understanding your
> point.

Definitely.

You should remember that Objective-C, the object parts, are very
dynamic: everything can be done at run-time.  You can create new classes
at run-time, you can define new methods at run-time, you can instanciate
those classes, and you can send messages at run-time, that weren't
defined at compilation time and that the compiler knows nothing about.

The only thing that is a little harder to do in C at run-time, is to
actually compile a new method body.  But using LLVM or dynamic loading
of libraries (that you may have compiled at run-time invoking an
external C compiler), you could also define new method bodies at
run-time.

Now, clearly, this is more difficult to do that in Objective-C than in
Smalltalk or Lisp (and Apple forbids it on iOS).  But this is still what
Objective-C is, and this is the big difference between Objective-C and
C++.



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