tags:

views:

43

answers:

2

im a newb to objective-c i am viewing this video and the guy is talking about mutli argument setting in methods. i come from a background of php and many other languages. ive studied c in the past and the foundation language is pretty much the same across all languages.

in a method like this:

- (void) setTo: (int) n over: (int) d {
....
}

how would this look like in another language? if this was C would this method be written like this?

void function setTo(int n, int d) {
..
}

but then i dont understand what over is and what its purpose is. its confusing. the only way i can understand this language is if i compare it to another.

thnx

A: 

Hi! I had exactly the same issue just today, as I just started learning Objective-C. Turns out "over" is a label for the argument d, so to invoke that method you would write:

[object setTo: 1 over: 2]

You can just omit it, but that is considered bad style:

-(void) setTo: (int) n : (int) d
...
[object setTo: 1 :2]

In the second case, what is the "2" referring to? Having a label makes it more clear.

Keep in mind that a label is part of the method name, so the two methods above are distinct methods actually. Hope this helps!

João da Silva
thats weird, thanks for that. would that label be called in the message call? and lol why is that considered bad style? it sounds so english when its written like that.
sarmenhbbb
Hi! I just read about this this afternoon, so it's fresh in my memory :) Please have a look here: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW6
João da Silva
No, you absolutely cannot omit part of the method name. It won't work, not at all. Which is sort of what your last sentence implies.
bbum
da Silva is correct. "setTo:over:" and "setTo::" are different messages, but you can declare and call "setTo::" as he describes. It is just that you'd have to be crazy to do it.
Dustin Voss
A: 

In Objective-C, method names have arguments thrown in the middle of them. I would say that for - (void)setTo:(int)n over:(int)d, a good analogous C function would be void function setTo_over_(int n, int d).

I've added underscores and the word "over" because the placement of the arguments and the word "over" are parts of the method name. The method's name is really setTo:over:. "setTo" is only the first half of the method name.

This is really useful when you're doing something like, for example, colorWithRed:1.0 green:0.5 blue:0.7. In C, this would be colorWithRed_green_blue_(1.0, 0.5, 0.7), and it's a little hard to tell which number is which. Thanks to Objective-C's ability to put arguments right in the middle of the method name, we can clearly see which number is which component of the color.

(Worse yet, because underscores look funny, and just because of convention, it's likely that a C function would be more like color(1.0, 0.5, 0.7), and now we're just completely confused. That is, what we usually use Objective-C's arguments-in-the-middle-of-a-method feature for is for fake "named arguments".)

You should check out Cocoa Style for Objective-C, Part I. It has a detailed explanation of good style when naming methods, and a bit of comparison between Objective-C methods and C functions.

andyvn22