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 16:39:16 -0500
- Organization: Latin AmeriCom, formerly Latino Online
Ivan Myrvold wrote:
>
My next dumb question to this group, for my Document Based application:
>
>
In MyDocument.h I have:
>
- (NSString *)niceMessage;
>
>
In MyDocument.m I have:
>
- (NSString *)niceMessage {
>
return @"Nice Greeting!";
>
}
>
>
In MyWindowController.m in one of my methods:
>
>
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)
>
>
And running the application, I get the following in the console:
>
The Message: (null)
>
>
The documentation says:
>
>
As a convenience, the Objective-C language also supports the @"..."
>
construct to create ....
>
Such an object is created at compile time and exists throughout your
>
program's execution. The compiler makes such an object constants unique
>
on a per-module basis, and they're never deallocated (though you can
>
retain and release them as you do any other object).
>
>
So why does I get a null in the console, and what about the compiler
>
complaints?
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.
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. If, instead, it is "nil" (no document associated with this
controller, at this time), you will get the result you express. (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.)
So check that the [self document] call actually returned an instance of
MyDocument.
David email@hidden