objective-cmethod-names

Bizzare method signature, with unnamed arguments (obj-c)


I wasn't aware this syntax was valid.

+ (void) methodName:(TypeObject *)typeObject1:(TypeObject *)typeObject2;

Which is then called like so:

[object methodName:obj1:obj2];

I find it ugly and disturbing, but it builds.

Can someone point me at a reference which explains why this is valid.

FWIW the codebase (inherited) that this comes from, is rife with sloppy, lazy stuff, dozens of spelling errors and looks like it was formatted by someone with no need to ever read it again. (Thank you again uncrustify.)


Solution

  • This is a well-kown and documented feature (pdf, p. 14)

    In principle, a Rectangle class could instead implement a setOrigin:: method with no label for the second parameter, which would be invoked as follows:

    [myRectangle setOrigin:30.0 :50.0]; // This is a bad example of multiple parameters

    but apple discourage everbody of using parameter passing without keyword:

    Use keywords before all arguments.
    - (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag; -> Right.
    - (void)sendAction:(SEL)aSelector :(id)anObject :(BOOL)flag; -> Wrong.

    Why it was allowed by the creators of objective-C, I dont know. Maybe it has to do with the Smalltalk heritage.