Re: A niceMessage in MyDocument
Re: A niceMessage in MyDocument
- Subject: Re: A niceMessage in MyDocument
- From: "David W. Halliday" <email@hidden>
- Date: Tue, 19 Jun 2001 22:48:36 -0500
- Organization: Latin AmeriCom, formerly Latino Online
Art Isbell wrote:
>
On Tuesday, June 19, 2001, at 11:39 AM, David W. Halliday wrote:
>
>
> Ivan Myrvold wrote:
>
>
>
>> NSLog(@"The Message: %@", [[self document] niceMessage]);
>
>>
>
>> The compiler complaints about the above, saying:
>
>> warning: cannot find method (pointing to the NSLog line).
>
>> warning: return type for 'niceMessage' defaults to id (for the same
>
>> line)
>
>
>
> First, the compiler complaints are normal, since, with the
>
> way you are
>
> using the document in your MyWindowController class, the
>
> compiler doesn't
>
> know that the type of the document is (MyDocument *) (the
>
> return type of
>
> the "document" message, to MyWindowController class, is "id").
>
> You can use
>
> a cast to get rid of these warnings.
>
>
I don't think this is quite correct. The message states
>
that the niceMessage method cannot be found which is likely due
>
to MyDocument.h not being imported in MyWindowController.m.
>
Because Objective-C is a dynamic, late-bound language, the
>
compiler can't know and doesn't care what [self document] might
>
return. Any number of classes might implement a niceMessage
>
method (e.g., many classes define a "description" method - i.e.,
>
polymorphism). It just needs to know the return type of a
>
niceMessage method (there cannot be multiple niceMessage
>
declarations with different return types).
Actually, different classes /can/ have different implementations of
"niceMessage" that /can/ have different return types, so long as they are in
different branches of the inheritance tree (not that such is desirable, just
that this is allowed).
Actually, the compiler can know the /declared/ return type of [self
document], in exactly the same way as for "niceMessage".
However, I was confusing the warnings with those received when the type
is static, rather than "id".
I made myself a little test case, and though I do get the same warnings,
I get the desired result---not (null). (I even made one with two subclasses
with a "notNiceMessage" that returned different object types. It still
worked. [Admittedly, if the return type were a primitive C type, rather
than an object, we would be in deep trouble :-/.])
>
>
> However, these should have no affect upon the result of the
>
> program,
>
> provided (and this is the crux of the matter) the actual type of the
>
> document returned by the "document" method is an instance (actually,
>
> pointer to an instance, but why quibble) of MyDocument, or a subclass
>
> thereof.
>
>
or of any class that implements niceMessage - not
>
necessarily a kind of MyDocument.
Certainly. I simply assumed he didn't have any other such classes.
However, it is often good to point out the general case, since he is
probably not familiar with such.
>
>
> If, instead, it is "nil" (no document associated with this
>
> controller, at this time), you will get the result you express.
>
>
True.
>
>
> (Another
>
> possibility that can cause trouble, but would have raised an exception
>
> [right?], is if the type of the document associated with this Window
>
> Controller is not an instance of MyDocument, or a subclass thereof.)
>
>
Not true. The document object returned by sending self a
>
document message must merely reply to a niceMessage message or
>
be able to forward this message to another object that replies
>
to this message. If neither is true regardless of the class of
>
the object (is C++ with its very limited polymorphism in your
>
background ?-) then an exception will be raised which will lead
>
to a crash if uncaught.
Well, again, I was assuming he had no other classes that responded to
that message. However, again, it is good to point out the more general
case.
(C++ is in my background, even though I learned it /after/ learning
Objective-C [thank goodness :-)]. However, perhaps because of this, and so
much programming in Java, of late, my Objective-C is a little bit on the
rusty side---hence the uncertainly as to whether the lack of a reasonable
response to the "niceMessage" would raise an exception [I thought I
remembered it would, I just had some self doubt :-)].)
I was simply thrashing about to see if there could be any alternate
explanation for his printing "(null)".
>
>
> So check that the [self document] call actually returned an
>
> instance of
>
> MyDocument.
>
>
or any object that responds to a niceMessage message.
Certainly.
>
>
>
Art Isbell
>
...
Sorry for any confusion my assumptions may have caused.
David email@hidden