Re: Collection of Cocoa & objc questions from a "newbie"
Re: Collection of Cocoa & objc questions from a "newbie"
- Subject: Re: Collection of Cocoa & objc questions from a "newbie"
- From: "David P. Henderson" <email@hidden>
- Date: Mon, 16 Jul 2001 21:25:36 -0400
On Monday, July 16, 2001, at 07:21 , tyler wrote:
>
So I've been working through the Learning Cocoa book and have
>
generated the following questions regarding the Dot View example
>
application in Ch 8.
>
>
Several curious things.
>
>
1) Objective C: it appears that methods do not really need to be
>
declared in the .h file of objective C files in order for them to get
>
compiled and called by other classes.
>
That is because Objective-C is an extension of C. See below.
>
1a) the inherited methods awakeFromNib, initWithFrame and so on seem to
>
get called for the DotView custom view even though I chose not to type
>
them into the DotView.h file.
>
Because the runtime expects them. Objective-C is a dynamic language in
most instances decisions about which method an object performs are made
at runtime not compile.
>
1b) the custom methods I defined that controls are using as action
>
methods seem to be callable without declaring them in the DotView.h
>
file as well.
>
Again because Objective-C is just an extension. See below.
>
1c) the DotView.m file compiles just fine with methods that are NOT
>
declared in the .h file.
>
See below
>
How is this possible? I'm used to c/c++/pascal/etc that require a
>
declaration to even compile.
>
C, and by extension Objective-C do not require declaration. In C, if you
can live with compiler warnings, you can do implicit declaration. C++
requires explicit declaration. Do not confuse declaration with
prototyping. In C:
int main(int argc, char *argv[])
{
something(); // something is implicitly declared here as int
something(void); Compiler will complain
}
void something(void) // something is now redeclared explicitly here;
Compiler will complain again about this
{
...
}
results in the compiler issuing warnings about 1) implicit declaration
and 2) something not matching its declaration, but neither warning is
fatal to the compile.
If I were to retype the source such that I define something before I
define its first use, the warnings go away since the definition also
serves as declaration. Don't confuse prototyping with declaration.
Prototyping is useful if you are publishing an interface to your class
or functions. Prototyping can also serve as and is most often used as a
declaration for a function or method.
>
>
2) [snip]
>
I don't understand the issues surrounding the objc runtime or nib
interaction with such to give a decent answer to this item.
>
3) If I set a breakpoint in the (void)dealloc method for the DotView
>
custom view, it is never called (close window, quit app, never called).
>
Then you are not explicitly releasing your dotview object.
>
Does this mean that code in a dealloc method may never be called in
>
general and shouldn't be counted on getting called?
>
No, it means you have to explicitly release an object to cause its
dealloc method to be performed.
Dave
--
Chaos Assembly Werks
"The proper office of a friend is to side with you when you are in the
wrong. Nearly anybody will side with you when you are in the right."
- Mark Twain