Re: beginning Obj-C [LONG]
Re: beginning Obj-C [LONG]
- Subject: Re: beginning Obj-C [LONG]
- From: Piers Uso Walter <email@hidden>
- Date: Thu, 7 Aug 2003 23:48:03 +0200
Am Donnerstag, 07.08.03 um 12:17 Uhr schrieb David Thorp:
id anObject;
might be like saying
variable age;
but not saying what type (int, char, whatever) of variable it is. Then
at a later time in the program, I can somehow tell the program that
this particular age variable for the time being can be an int. But
then later maybe it can be a float or char instead.
I think you more or less got it, but you may not know it yet ;-)
It may help to mentally replace all occurances of 'id' by 'NSObject *',
because that's more or less what id is (which should also answer your
secondary question).
In Objective-C it also helps to not think of pointers to objects as
indirect references (as you would in C), but rather think of them as
'the objects'. For most purposes, a pointer to an object "is" the
object. You use the pointer all over the place, e.g. when sending
messages to the object: [anObject doStuff].
Viewed from a C perspective, "id anObject" declares anObject to be a
pointer to an NSObject instance.
In Objective-C, you may instead think of "id anObject" declaring
anObject to "be" an NSObject instance. If you look at it in detail,
this view is not exactly right, but I've found that it makes reading
and writing Objective-C programs much easier than always thinking of
the pointers. I'm using "type" (in quotes) below to indicate this view
as opposed to type (without quotes) for the technically correct,
C-style view.
From a C perspective, anObject's type does not change, regardless of
what values you assign to anObject (no matter where it points to, the
pointer always remains a pointer).
From an Objective-C perspective, anObject's "type" may indeed change
when different values are assigned to anObject, because the "type" of
anObject reflects the type of the instance that anObject points to.
I hope that this helps a bit instead of further adding to the confusion.
With kind regards
Piers Uso Walter <email@hidden>
ilink Kommunikationssysteme GmbH
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.