beginning Obj-C [LONG]
beginning Obj-C [LONG]
- Subject: beginning Obj-C [LONG]
- From: David Thorp <email@hidden>
- Date: Thu, 7 Aug 2003 20:17:24 +1000
Greetings again all...
Firstly, this is rather long, and for that I apologise. If you don't
read it I won't be offended :-)
Secondly, one person on this list emailed me privately to ask me to
find another forum for seeking help with my C questions. I'm inclined
to disagree with that suggestion, but I don't want to upset anyone so
I've tried to limit the questions anyway. Thankfully though, I think
I'm pretty clear on C now anyway.
But now I've moved onto Objective-C and I know of no other forums that
would be appropriate for seeking help on that. If anyone objects to
these questions (since they're Obj-C and not specifically Cocoa) please
let me know. I'll welcome any suggestions as to where I else I could
take these (and possible future) Obj-C questions.
All that said... I will be very grateful to any of you who are inclined
to read through all this and help me in getting over a couple of early
humps I've hit in understanding Objective-C.
History:
As most of you probably know by now I'm on the learning path from
having done a Pascal and "good programming techniques" course at
university 10 years ago, then spent 10 years programming in FileMaker,
to now deciding to learn Cocoa and do some "real" :-) Mac programming.
As recommended by some of you, I've been learning C (and have pretty
much got my head around that now I think, and on the way to Cocoa, I'm
now going through Apple's document on Objective-C. (It's on their
website <developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC>
but I've downloaded and printed out the pdf (ObjC.pdf)).
Current problem:
So right now, I believe I've pretty much fully understood up to the end
of chapter 2 ("Object-Oriented Programming") of that ObjC.pdf file, but
am struggling in Chapter 3 ("The Objective-C Language"). Here's what I
think I've figured out so far...
C (like most languages) has built in types. We declare variables of a
certain type, and then give values to (initialize) them eg:
int age; //declaration
age = 30; //initialization
A particular kind of type that is very useful of course is a struct - i
see that as a programmer-definable type - would that be accurate?
Now... objects/classes. Conceptually, I'm currently thinking of an
object as like a glorified struct (programmer-definable type). A
struct has to be defined, but after that it's like any other type - we
declare the variables and then initialize them (as outlined above).
Like a sruct, an object is essentally a set of variables (data
structure) and then some. (I realise that unlike a struct an object
has a set of methods to tell the data what to do. And the data
structure is hidden from outside of the object - instead accessed only
through the methods. All combined, it makes up an object. - I
appreciate there's a lot more to it than that, but go with me for a
second if that's ok). Is this all pretty much right so far?
Now, I lost it right on page 56 I think (second page of chapter 3) with
"id". Although perhaps I am just grasping it. I'm beginning to think
the idea of:
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 realise of
course that that's not normally done in C, I'm just using it as an
analogy.
The analogy is: Is saying "id anObject;" merely declaring an object
without saying what type of object it is, simply because objective-C
allows us to change what type of object it might be during runtime? Is
that it?
I feel as if I were explaining this I would have explained first that
one can define a class (eg. "class Rectangle;"), kind of like defining
a struct - programmer-definable type, then I would have explained how
to declare an object of a certain class (eg. "Rectangle myRect;") kind
of like declaring variables of your defined struct, and then later
explained this extra cool feature of being able to declare an object
without saying what class it is if you want (eg. "id myRect;"). But
then of course is the possibility that I'm way off base and got it all
wrong.
So the big question is, from my explanation above, have I understood
this correctly, or am I way off base?
A secondary question is: I also note, that (at least in the pdf) it
declares "id anObject;" (no * to indicate pointer) but "Rectangle
*myRect;" (with a * to indicate pointer) why the * in "Rectangle
*myRect;" but none in "id anObject;"? (Although that again may be
because I've completely misunderstood).
Again, sorry for the long email, but if any of you can help me
understand this I would be most grateful. Right now I'm pretty
confused. If you've managed to read this far let me give you my utmost
thanks at least for ploughing through it!
In responding, please understand that I am very new to this, so new
terminology outside of C terminology will probably lose me.
Thanks again in advance.
David.
_______________________________________________
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.