tags:

views:

49

answers:

4

i am viewing this video on objective c. the guy show a method which takes multiple arguments and it looks like this

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

to use it he shows:

[myFraction setTo: 100 over:200];

how would that bracket notation look in dot noation? andi dont understand what that over means, would anyone know? thnx

A: 

From the looks of it, this method is setting two instance variables/properties of the object the numerator (n) and the denominator (d). You cannot do this using dot notation unless you break it into two calls:

myFraction.numerator=100;
myFraction.denominator=200;

Note that dot notation is only for accessing instance variables/properties and not an alternative to message sending.

Eyal Redler
This is wrong! First, dot notation does NOT access instance variables, and second, it is EQUIVALENT to message sending: `myFraction.numerator = 100` is exactly the same as `[myFraction setNumerator:100]`.
andyvn22
The dot syntax is intended only to be used for accessor methods which are the way to access instance variables/attributes. Any other use is not recommended (although it may be possible: see the section "Incorrect Use" under "Dot syntax" in Apple's "The Objective-C Programming Language". Maybe I wasn't clear enough in my original answer but I was not completely *wrong*, I think you could have used a more polite tone...
Eyal Redler
+1  A: 

You cannot pass multiple arguments with dot notations. A setter usable by dot-notation must have the prototype

-(void)setXxxx:(type)value;

However, you can create an auxiliary struct to group all arguments into one:

struct Fraction { int n, d; };
struct Fraction MakeFraction(int n, int d) {
   struct Fraction r;
   r.n = n;
   r.d = d;
   return r;
}
...
-(void)setValue:(struct Fraction)f { ... }
...
myFraction.value = MakeFraction(100, 200);

(and a "over" b means a / b.)

KennyTM
+1  A: 

The dot notation is a shorthand notation for property access only. The compiler translates it to the appropriate setter/getter method call. It is just syntactic sugar.

So given this property access using dot notation:

myFraction.numerator=100;

The compiler replaces it with the following equivalent code:

[myFraction setNumerator:100]

Now it should be clear why you cannot use dot notation for sending a normal message to an object. I can't even think of a way how that should even look like.

There is a lot of discussion concerning dot vs. bracket notation going on. One of the arguments against dot notation is the confusion it generates especially for beginners. Other languages do of course use methods for property accessors too, however they hide this fact more consistently than Objective-C.

Johannes Rudolph
I'm not sure if you meant 'property' in the `@property` sense, or in a more general one, but please do note that dot notation works just fine with handwritten accessors (even if `@property` is never declared in the .h). And I'd even say it's not poor style to use dot notation for something like `myArray.count`, if you wanted.
andyvn22
A: 

In Objective-C, we can name methods with arguments in the middle. So when he writes [myFraction setTo:100 over:200];, the over could mean... well, whatever he wants. It's part of the method name he chose. In this case, he was probably trying to make the method sound like English. ("Set this fraction to 100 over 200." We read fractions as "numerator over denominator" often in normal speech.)

Some methods, called "accessors", we write very frequently: these are methods of the form - (int)variable (called "getters"), and - (void)setVariable:(int)newValue (called "setters"). My examples here would assumedly return, or change, respectively, an instance variable called variable. Here's what the method implementations might look like:

- (int)variable
{
    return variable;
}

- (void)setVariable:(int)newValue
{
    variable = newValue;
}

It's common to have accessors like this for almost every instance variable your class has. At some point, someone got tired of writing [myInstance setVariable:20]; and such, and decided they'd rather it look like many other languages out there, myInstance.variable = 20;.

Therefore, Objective-C 2.0 added dot notation, which allows you to write...

myInstance.variable, which is exactly equivalent to [myInstance variable] in most circumstances (and does NOT access the instance variable variable directly!), and...

the special case myInstance.variable = 20;, which is exactly equivalent to [myInstance setVariable:20];. Again, note that this does not access variable directly, it sends a message to myInstance. So if we'd written some other code in setVariable, it would still be accessed if we used dot notation.

Dot notation is designed to be used for accessors. You could also theoretically use it for any method that returns a value and takes no arguments (myArray.count, for example). Using it for anything else (myInstance.doSomeAction) is extremely poor style. Don't do it. Therefore, as I'm sure you can guess by now, [myFraction setTo:100 over:200] has no dot notation equivalent, as it takes 2 arguments, and isn't an accessor.

andyvn22
i come from a php background and i have learned many other languages in the past. when i look at a method in objective c i compare it to a function in php or whatever i can recal from learning c way in the past. how can this - (void) setTo: (int) n over: (int) d be compared in a language like c? void function setTo(int n,int d) { } where does over: come in??
sarmenhbbb
`over:` is part of the method name. `setTo(int n, int d)` is not a good analogous C function. `setTo_over_(int n, int d)` is better. The full name of the method in question is not `setTo`, it is `setTo:over:`. The arguments just happen to be in the middle of the method.
andyvn22