开发者

method taking mutli arguments in dot notation

i am viewing this video开发者_StackOverflow社区 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


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.


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.)


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.


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.


[myFraction numerator]

Could be written as

myFraction.numerator

but you can also assign values such as

instance.property = value

But you cannot pass multiple arguments in dot notation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜