Re: star questions
Re: star questions
- Subject: Re: star questions
- From: Enrique Zamudio Lopez <email@hidden>
- Date: Fri, 22 Jun 2001 16:20:02 -0500
All objects in Cocoa (well, actually in ObjC) are dynamically allocated,
so you can only have pointers to objects in your code. So, yes, the "*"
is used as in C, to indicate a pointer.
id is a pointer to an object of any class. You can have a pointer of a
specific class by using the "Class *variable" declaration, as in:
NSAutoreleasePool *pool;
There you have a pointer to a NSAutoreleasePool. Just as in C, the space
is optional, so all of these are the same:
NSAutoreleasePool* pool;
NSAutoreleasePool*pool;
NSAutoreleasePool *pool;
NSAutoreleasePool * pool;
Also, the default type for parameters and for return types in methods is
id. So if you want to take any other type of parameter or return any
other type of parameter, you have to cast it:
- (void)setString:(NSString *)value;
- (NSString *)value;
- (void)setInteger:(int)value;
- (void)setFloat:(float)value;
These two are the same:
- setSomething:value
- setSomething:(id)value
although I think the use of declarations without any cast is
discouraged, so you should ALWAYS cast the parameters and return values,
even if it's to id.
You can think of the id type as a cast to (NSObject *) but the compiler
will check any method sent to an id-declared object against all known
classes at that moment. So sometimes you'll get warnings saying that
there are several methods with the same name but with different return
types or different parameters. Oh, and last but not least, the colons
are part of the name, so -method and -method: are not the same thing...
"Should it be so impossible that a man might supervise the ConstruKction
of Light?"
King Crimson