3

I'm new to Objective C.

While I am reading the tutorial here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingMethods.html#//apple_ref/doc/uid/20001282-BCIGIJJF

It says:

Use keywords before all arguments. Right way:

- (void)sendAction:(SEL)aSelector toObject:(id)anObject forAllCells:(BOOL)flag;

Wrong way:

- (void)sendAction:(SEL)aSelector :(id)anObject :(BOOL)flag;

It confuses me about Objective-C's method names. Here is my question:

1.

The wrong way is not recommended. But it's perfectly legal, right?

2.

The name(signature?) of the method is sendAction:toObject:forAllCells: for the first one and sendAction::: for the second one. Right? I noticed that people emphasize that the colon : is always counted as in the method name. I assume that : indicates an argument will follow and it cannot be modified anyway. So what's the implication of including colon in the method name as it's not subject to my modification.

3.

To make an example, - (void)sendAction :(SEL)aSelector;

So the name of the method should be sendAction :

Noticing the blank space before colon is part of the name, should I consider this as a different method from - (void)sendAction:(SEL)aSelector; ?

The answer should be NO as [anObject sendAction : anSel]; should be the same as [anObject sendAction:anSel];

How do you guys understand the scheme as a whole that make sense? Thanks.

P.S. Appreciate your reading through this question. I am pretty sure I am gonna feel silly once some of you pointed out and cleared my confusion.

Weishi Z
  • 1,629
  • 5
  • 18
  • 38

1 Answers1

2

The wrong way is legal, yes, but bad style. Just don't do it. The colons are part of what make ObjC code nice to read (though verbose); all the arguments at the call site have labels that remind you what they are for and often what type they should be.

The space is not in fact part of the method name; it's ignored. This is one of those things about ObjC that's pretty much "compiler-specified".

@interface Jackson : NSObject

- (void)shootCannon :(double)range;

- (void)grillSandwichWithBread :(BreadType)bread cheese :(CheeseType)cheese;

@end

@implementation Jackson
@end

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        NSLog(@"%@", NSStringFromSelector(@selector(shootCannon:)));
        NSLog(@"%@", NSStringFromSelector(@selector(shootCannon :)));
        NSLog(@"%@", NSStringFromSelector(@selector(grillSandwichWithBread:cheese:)));
        NSLog(@"%@", NSStringFromSelector(@selector(grillSandwichWithBread :cheese :)));

        NSLog(@"%d", @selector(shootCannon:) == @selector(shootCannon :));
        NSLog(@"%d", @selector(grillSandwichWithBread:cheese:) == @selector(grillSandwichWithBread :cheese :));

    }
    return 0;
}

2014-04-05 23:59:29.548 MethodNameSpace[66948:303] shootCannon:
2014-04-05 23:59:29.548 MethodNameSpace[66948:303] shootCannon:
2014-04-05 23:59:29.549 MethodNameSpace[66948:303] grillSandwichWithBread:cheese:
2014-04-05 23:59:29.550 MethodNameSpace[66948:303] grillSandwichWithBread:cheese:
2014-04-05 23:59:29.550 MethodNameSpace[66948:303] 1
2014-04-05 23:59:29.551 MethodNameSpace[66948:303] 1

Community
  • 1
  • 1
jscs
  • 63,694
  • 13
  • 151
  • 195