Re: *?
Re: *?
- Subject: Re: *?
- From: "David P. Henderson" <email@hidden>
- Date: Fri, 22 Jun 2001 22:40:45 -0400
On Friday, June 22, 2001, at 02:06 , email@hidden wrote:
I keep running into things like:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
and:
- (NSString *)airports;
- (void)setAirports:(NSString *)str;
Now, I understand the use of the * in:
NSMutableArray *myArray;
which says that myArray is of type "NSMutableArray" instead of generic
type "id". But there is no explanation that I can find as to what
the * does or means in the first two examples. My copy of Kernighan
and Ritchie just came and I looked there, but all I could find was a
usage somewhat similar on page 94 that uses the * to indicate that an
argument is a pointer to a type. So, all I can guess is that in the
example:
Bingo. All objects in Objective-C are pointers. 'id' is a pointer to an
object ie, any object, while ClassName * is a pointer to an instance of
ClassName. The difference in syntax between 'id myObject' and 'ClassName
*myObject' is that 'id' is defined by ObjC as a pointer whereas with
ClassName you have to explicitly declare the pointerness of 'myObject'.
- (NSString *)airports;
the * is shorthand for statically typing "airport" to NSString. Is
that right? If so, then in the second line:
- (void)setAirports:(NSString *)str;
"str" is statically typed to NSString also?
It is still a pointer. I think the use of the parentheses is throwing
you off. In C parentheses are also used to cast one type to another. The
usage here is similar. You are telling the compiler and anyone else
reading your code that you are expecting -airports to return an NSString
and that you are expecting -setAirports: to be passed an NSString. You
are hinting as to the nature of the data. You could pass any object in
or out and so long as it responds to NSString methods you'll be okay.
As for the first example:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
is this the same as:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];?
Is the space between the * and "pool" optional? If so is there some
reason to use the extra space in this construction?
Yes, the space is optional. The general rule is use white space to
enhance readability of your code. Many prefer the style of the second
example as more readable. You could also write it as
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
Sorry for the no doubt low level questions, but I'm trying to
understand this stuff and explanations are just not in the docs--or if
they are I have not been able to root them out.
Hopefully, I've helped clear it up some.
On Friday, June 22, 2001, at 04:22 , Finlay Dobbie wrote:
[pointers] are a fundamental thing that you need to understand, and are
not terribly easy to grok in the beginning.
I respectfully disagree. What is a pointer? A pointer is a reference to
the location of a value eg, a table of contents is filled with pointers,
an index is filled with pointers, ... To use a pointer you must
de-reference it ie, look up its value. In C and thus in ObjC, a pointer
is a reference to the location in memory of the value. The difficulty
with pointers comes in using pointers in more advanced ways.
The nice thing about ObjC is that you are generally shielded from
thinking in pointers since just about everything is one.
Dave
--
Chaos Assembly Werks
"Nothing is too good to be true, except, perhaps, the morality of a
bishop."
- Israel Zangwill
References: | |
| >*? (From: email@hidden) |