PDA

View Full Version : A few specific questions regarding syntax




jerrywickey
Apr 24, 2009, 08:01 AM
Just a check of my reading so far.

I couldn't find these simple explanations anywhere. 'seemed the docs wanted to explain to death the concept of OOP but never actually gets around to actually explaining the syntax which causes the OOP behavior.


Does:

1)
- (void) method: argument
{
}

cause the method method to be defined and called when ever the event connected to the method occurs or when this method is called by another method, but only from with in its parent?


2)
+ ( void) method: argument
{
}

cause same but can be called from outside its parent class?


3)
[self rotateArmToDegree:kDisplacemntAngle];

cause the value of the constant kDisplacementAngle to be passed as an arguement to the method rotateArmToDegree, while self simply identifies the method rotateArmToDegree as a method of the class defined in this implementation file .m ?


4)
[metronomeArm.layer removeAllAnimations];

Cause the method removeAllAnimations to be sent to the method layer of the class metronomeArm?


5)
Are methods called messages when they are passed to other methods?

6)
Or are the returned values of methods passed to another method called messages?

7)
Is capitilization entirely option?

8)
Is is optional in some cases only?


9)
#interface MetronomeView : UIView
{
}

cause the list of IBOutlets and other things between the brackets, to be made available as methods of the subclass MetronomeView of the superclass UIView?



Thank you for any help.

Jerry



BlackWolf
Apr 24, 2009, 09:30 AM
ok ... let's see.
first let me say: since cocoa touch is just a derivate of objective-c, you can find all those things when you look at objective-c tutorials (google them).
to answer your questions though:

1) I'm not quite sure what you mean. which event are you talkin about? what you posted simply defines a method called "method" with a single argument called "argument" - no more no less. the "-" means that it is an instance method, not a static method (If you don't know about static read about it in a tutorial of your choice, every programming language has static variables and methods).
Oh and btw: this isn't even a valid method declaration. you have to tell cocoa the type of your argument, so something like this is correct:

- (void) method:(NSArray *)argument
{
}


2) Besides the missing argument-type, THIS defines a static method, which means that it can be called without creating an instance of the class it belongs to. so if this method was defined in a class called "Foo" you can call it with [Foo method:parameterToPass]; ... again, if you don't know about static stuff google it, it's really a basic of every programming language and would take too long to explain here :D

3) Well, "kDisplacemntAngle" does not have to be a constant, but basically you are right. it calls the method "rotateArmToDegree" in the class you are currently in (self) and passes the parameter "kDisplacemntAngle" to it

4) No. Methods cannot be "spoken to" with the dot-operator. The Dot-Operator is used for properties. so if "metronomeArm" has a property (for example an array, it has to be an object in any case, so an instance of a class) called "layer" and that property has a method called "removeAllAnimations" this is what gets called. Btw, but this is just tiny, but you don't send methods, you send messages

5+6) "messages" are sent if you use [], basically. so [someObject methodOfObject]; sends the message "methodOfObject" to "someObject" which, in the end, causes the method "methodOfObject" to be executed. BUT a message can also be:
int foo = [someObject someProperty];
which is basically the same as
int foo = someObject.someProperty;
this calls the getter-method of "someProperty", which returns the value of someProperty. everytime you set or get a property it is nothing more than a method being called (even if you don't have to write the method yourself), so it's a message as well.

7+8) depends. someProperty is not the same as someproperty, so objective-C IS case-sensitive, if that's what you mean. for your own methodnames or variable names you can use whatever you want of course, though I personally stick to apples kind of naming things (so I use camelCase)

9) best thing here: download a simple sample code from apple and look at an interface. an interface has various parts. methods, for example, are not defined between the brackets but below them. like I said, look at an example, this is the best way to do it.

regards

jerrywickey
Apr 28, 2009, 08:35 AM
Thank you very much

Your help is very useful

Jerry

firewood
Apr 28, 2009, 10:13 AM
So when are these exam questions due?

jerrywickey
Apr 28, 2009, 10:20 AM
Not an exam.

Check out my other posted questions.

I am way too old for school. I am an old assembly language programmer who needs to get with the times and learn OOP.

Jerry

CocoaPuffs
Apr 28, 2009, 10:22 AM
Not an exam.

Check out my other posted questions.

I am way too old for school. I am an old assembly language programmer who needs to get with the times and learn OOP.

Jerry
Your questions sound like you have been doing some old HTML programming.

firewood
Apr 28, 2009, 10:26 AM
For 1 & 2.

You need to read up on the difference between a class and an object.

Object methods require that some bits of data memory actually be allocated first. "self" can be used when those bits "want" to refer to themselves, and not some other bunch of bits.

There is also a difference between the rules for a language's syntax, and the style guidelines. You can write a perfectly correct Obj-C program using completely awful and unreadable style. Or you can capitalize your bugs very elegantly.

firewood
Apr 28, 2009, 10:47 AM
Both methods and messages are just names for fancy subroutine calls. Think JSR register indirect. The only fancy stuff is that which machine code address that actually gets called might get filled in much later (e.g. maybe well after the program starts running), and might actually be a stack of calls. The first parameter, a pointer, isn't in the parameter list. It's stuck out there in front of the subroutine call. Access it as "self" (or "this" in some other language).

An object is a bunch of fancy nomenclature for stuff that allocates and helps initialize and manage those bits referenced by that magic pointer that isn't in it's usual place on the subroutine parameter list.

o