PDA

View Full Version : Some clarification about method calls




counciler
Jan 9, 2011, 12:24 AM
I'm still pretty new to this programming stuff. So I've picked me up some books and have learned quite a bit. Now, I understand in objective C, in order to call a method you use the [class method] format. But what confuses me is there are some things in iPhone/iPod/iPad development that doesn't seem to follow this. For example:

[[touches anyObject] locationInView:self.view]

"touches" isn't a class but it works to call "anyObject." Another example is:

[[event allTouches] anyObject]

Where does "event" come from? I try looking at the documentation by double clicking on the name of this "class" and all it brings up for "touches" is for "touchesBegan:withEvent:", and "event" comes up with nothing. This is seriously confusing me.



JustSomeDude
Jan 9, 2011, 01:58 AM
I'm still pretty new to this programming stuff. So I've picked me up some books and have learned quite a bit. Now, I understand in objective C, in order to call a method you use the [class method] format. But what confuses me is there are some things in iPhone/iPod/iPad development that doesn't seem to follow this. For example:

[[touches anyObject] locationInView:self.view]

"touches" isn't a class but it works to call "anyObject." Another example is:

[[event allTouches] anyObject]

Where does "event" come from? I try looking at the documentation by double clicking on the name of this "class" and all it brings up for "touches" is for "touchesBegan:withEvent:", and "event" comes up with nothing. This is seriously confusing me.

You also use this syntax for calling a method on an object - a particular instance of a class. So touches and event would be instances of their respective classes. Look for the documentation for the class associated with these objects and you'll find the methods there. You may want to review the material here (http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW1).

counciler
Jan 9, 2011, 06:43 AM
So touches and event would be instances of their respective classes.

Yeah, already knew this. I was more curious why I was using these instances and had no idea where they came from, then I looked back at the code and looked at the actual method I was in and saw:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

So I guess I just needed to take a step back for a while. My head started to get really bogged down and I got pretty frustrated. What added to my confusion is each touch method I override uses "touches" and "event" as the instance name, so I was seeing them in various methods throughout my program. But now, life makes sense again, thanks!