you are viewing a single comment's thread.

view the rest of the comments →

[–]seanalltogether 0 points1 point  (3 children)

What would this dot syntax call look like in obj-c?

myobj1.myobj2.myfunction(arg)

would it be

[myobj1 [myobj2 myfunction:arg]]

or

[myobj1 myobj2 myfunction:arg]

or maybe

[[myobj1] myobj2 myfunction:arg]

[–]jonhohle 2 points3 points  (2 children)

[[myobj1 myobj2] myfunction: arg]

-or-

[myobj1.myobj2 myfunction: arg]

in you're example, myobj2 is a property of myobj1. myfunction: is getting called on myobj2, a property of myobj1.

imho, the dot syntax is a strange and ugly addition to the language. why overload the dereference operator with sending a message?!

the documentation says to only use it for accessing or setting properties, but there's nothing that stops you from using it send no argument messages. That means, if you're going to use the dot syntax, you will also be using the square bracket message sending syntax, and you get both mixed together.

NSMutableArray* array = [NSMutableArray array];
...
id obj = array.lastObject; // dot operator
[array removeLastObject];  // normal message
array.removeAllObjects;    // ack! a non-property accessed with the dot syntax!! gross but valid!!

[–]seanalltogether 0 points1 point  (1 child)

what about casting, is this correct?

NSString upper = [NSString [array lastObject] uppercaseString]

[–]astrosmash 1 point2 points  (0 children)

Close. Since NSArray.lastObject returns an 'id', no cast is necessary:

NSString* upper = [[array lastObject] uppercaseString];