Re: Are method declarations just comments?
Re: Are method declarations just comments?
- Subject: Re: Are method declarations just comments?
- From: Gerard Iglesias <email@hidden>
- Date: Sun, 8 Jul 2001 11:35:55 +0200
Le Sunday 8 July 2001, ` 08:52, Chris Gehlker a icrit :
Hi everyone,
I'm pretty new to Cocoa, just working my way through "Learning Cocoa" in
fact. I noticed that the book was having me add methods in .m files
without
declaring them in the corresponding .h file and the programs were still
compiling and running w/o warnings. So I went back and commented out
method
declarations and even changed them so they declared the wrong type of
arguments. Again it didn't seem to matter. I'm beginning to fear that
they're just comments. Someone say it isn't so.
Maybe this is one of the reason ObjC guys love their language. In a
header file of an Objc class you declare only the methods that you want
to be known by users of the class. It is absolutely not the same as Java
or C++ where people know everything about the class even the code (made
me laugh ;-)).
Hence, with ObjC class declaration you have to think about what you want
to exhibit to other people.
Nevertheless, if you declare a method in a class that is not implemented
then the compiler warn, saying that such method is not implemented.
Here an example of a typical way of ObjC coding:
The header file (.h) -->
@interface MyClass : NSObject
{
// some var instance declaration (can't be hidden for obvious
compiler reasons)
}
- (returnType)aPublicMethodDecl:(parType)par;
..
...
@end
The implementation file (.m) -->
// first declare a category containing some hidden method declaration
(it is not necessary but it is better)
@interface MyClass (MyClassHiddenDeclarationCategory)
- (returnType)aHiddenMethodName:(parType)par;
...
...
@end
// Implementation of MyClass
@ implementation MyClass : NSObject
- (returnType)aPublicMethodDecl:(parType)par
{
// the implementation
...
}
..
...
@end
// then the implementation of the hidden methods
@ implementation MyClass (MyClassHiddenDeclarationCategory)
- (returnType)aHiddenMethodName:(parType)par
{
// the implementation
}
...
...
@end
Well, this is only a small example, you are really free to do a very
different thing, but this is a way taken by a lot of them to do thing
with ObjC, take a look at the header file of Cocoa and look at the heavy
use of categories, that permit to do separate compilation, methods
organization, for example look at the header of NSView.
Have fun with Cocoa.
Gerard