Re: A niceMessage in MyDocument
Re: A niceMessage in MyDocument
- Subject: Re: A niceMessage in MyDocument
- From: Art Isbell <email@hidden>
- Date: Tue, 19 Jun 2001 14:28:35 -1000
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).
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.
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.
So check that the [self document] call actually returned an
instance of
MyDocument.
or any object that responds to a niceMessage message.
Art Isbell
Apple iServices Technical Support
http://www.apple.com/iservices/webobjectssupport/
+1-808-591-0836