我是Objective 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.
不建议使用错误的方式。但它是完全合法的,对吧?
阿拉伯数字。
该方法的名称(签名?)是 sendAction:toObject:forAllCells: 对于第一个,sendAction::: 对于第二个。右?我注意到人们强调冒号:总是算在方法名称中。我假设 : 表示将遵循一个参数,无论如何都无法修改。那么在方法名称中包含冒号意味着什么,因为它不受我的修改。
3.
举个例子, - (void)sendAction :(SEL)aSelector;
所以方法的名称应该是 发送操作 :
注意到冒号之前的空格是名称的一部分,我是否应该将其视为与 - (void)sendAction:(SEL)aSelector 不同的方法;?
答案应为否,因为 [anObject sendAction : anSel];应与 [anObject sendAction:anSel] 相同;
你们如何理解整个有意义的计划?谢谢。
附言感谢您阅读这个问题。我很确定,一旦你们中的一些人指出并清除了我的困惑,我会感到愚蠢。
错误的方式是合法的,是的,但风格不好。只是不要这样做。冒号是使 ObjC 代码易于阅读的一部分(尽管冗长);调用站点上的所有参数都有标签,提醒您它们的用途以及它们应该是什么类型。
空格实际上不是方法名称的一部分;它被忽略了。这是关于 ObjC 的事情之一,几乎是"编译器指定"的。
@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 方法命名空间[66948:303] 射击大炮:
2014-04-05 23:59:29.548 方法命名空间[66948:303] 射击大炮:
2014-04-05 23:59:29.549 方法命名空间[66948:303] 烧烤三明治面包:奶酪:
2014-04-05 23:59:29.550 方法命名空间[66948:303] 烧烤三明治配面包:奶酪:
2014-04-05 23:59:29.550 方法命名空间[66948:303] 1
2014-04-05 23:59:29.551 方法命名空间[66948:303] 1