2014-04-05 21 views
0

的使用我的新目標C.目標C方法的命名規則 - 結腸

雖然我在這裏讀教程:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingMethods.html#//apple_ref/doc/uid/20001282-BCIGIJJF

它說:所有參數之前

使用關鍵詞。 正確的做法:

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

錯誤方式:

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

它混淆了我關於Objective-C的方法名。這是我的問題:

1.

不推薦使用錯誤的方法。但這是完全合法的,對嗎?

2.

名稱(?簽名)方法的是sendAction:toObject:forAllCells:用於第一和一個sendAction :::爲第二個。對? 我注意到,人們強調冒號:總是作爲方法名稱計數。 我假設:表示參數將會跟隨,無論如何都不能修改。那麼在方法名中包含冒號的含義是什麼,因爲它不受我的修改。

3.

爲了使一個例子,- (無效)sendAction:(SEL)aSelector;

因此該方法的名稱應該是sendAction:

注意到空白處前冒號是名稱的一部分,我認爲這是從不同的方法 - (空)sendAction:(SEL) aSelector;

答案應該是NO [anObject sendAction:anSel];應該與[anObject sendAction:anSel]相同;

你們如何理解整個計劃是有意義的? 謝謝。

P.S.通過這個問題欣賞你的閱讀。我很肯定,一旦你們中的一些人指出並清除了我的困惑,我會感到無聊。

回答

1

錯誤的方式是合法的,是的,但風格不好。只是不要這樣做。冒號是使ObjC代碼很好讀取的一部分(儘管冗長);在呼叫站點的所有參數都有標籤,這些標籤可以提醒你他們的用途,以及他們應該是什麼類型。

該空間實際上並不是方法名稱的一部分;它被忽略了。這是關於ObjC的那些事情之一,這幾乎是"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年4月5日23時59分29秒。548 MethodNameSpace [66948:303] shootCannon:
2014年4月5日23:59:29.548 MethodNameSpace [66948:303] shootCannon:
2014年4月5日23:59:29.549 MethodNameSpace [66948:303] grillSandwichWithBread:乾酪:
2014年4月5日23:59:29.550 MethodNameSpace [66948:303] grillSandwichWithBread:奶酪:
2014年4月5日23:59:29.550 MethodNameSpace [66948:303] 1
2014年4月5日23 :59:29.551 MethodNameSpace [66948:303] 1