除了委托对象之外没有参数的委托方法的正确命名约定?
Proper naming convention for a delegate method with no arguments except the delegating object?
我有一个叫 ABCCalendarView
的 class。它需要一个 NSCalendar
,并从它的委托中获取这个对象。我正在尝试找出如何调用此委托方法,并且我想适应 Cocoa 框架的最佳实践。
根据the 'Naming Methods' section of Apple's 'Coding Guidelines for Cocoa',我应该:
Start the name by identifying the class of the object that’s sending the message:
- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(int)row;
- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;
The class name omits the prefix and the first letter is in lowercase.
A colon is affixed to the class name (the argument is a reference to the delegating object) unless the method has only one argument, the sender.
- (BOOL)applicationOpenUntitledFile:(NSApplication *)sender;
这表明我应该调用方法:
- (NSCalendar *)calendarViewUsesCalendar:(ABCCalendarView *)calendarView;
但是,查看 Cocoa 框架中这种情况的实际示例,我可以看到实际使用的约定似乎更喜欢将 class 名称放在方法名称的末尾这个案例。例如:
// UITableViewDataSource has the method:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
// UITableViewDelegate has:
- (NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView
// UIScrollView has:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
如果我遵循这个建议我应该调用我的方法的约定:
- (NSCalendar *)calendarForCalendarView:(ABCCalendarView *)calendarView;
为什么 Apple 的指导方针与他们的实际做法之间似乎存在差异?在创建基本上只是设置调用对象的 属性 的委托方法时应该遵循什么约定?
我认为 rules document 内部有点不一致。有两个 "General Rules" 其中状态:
If the method returns an attribute of the receiver, name the method
after the attribute.
和
Make the word before the argument describe the argument.
这些以及 SDK 名称中的先例都支持您的第二种表述:
- (NSCalendar *)calendarForCalendarView:(ABCCalendarView *)calendarView;
我认为 SDK 名称中有充分的证据表明您在委托部分找到的规则应该重述。正如你指出的那样,它说
Start the name by identifying the class of the object that’s sending the message
但我认为它应该说:"Send an instance of the caller as the first parameter"。这是一半命名规则,一半设计模式。这样,那些返回委托名称的单个参数、属性可以采用 returnTypeSenderType:
的形式(实际上是这样),而不会 运行 与文档的其他部分发生冲突。
我有一个叫 ABCCalendarView
的 class。它需要一个 NSCalendar
,并从它的委托中获取这个对象。我正在尝试找出如何调用此委托方法,并且我想适应 Cocoa 框架的最佳实践。
根据the 'Naming Methods' section of Apple's 'Coding Guidelines for Cocoa',我应该:
Start the name by identifying the class of the object that’s sending the message:
- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(int)row; - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;
The class name omits the prefix and the first letter is in lowercase.
A colon is affixed to the class name (the argument is a reference to the delegating object) unless the method has only one argument, the sender.
- (BOOL)applicationOpenUntitledFile:(NSApplication *)sender;
这表明我应该调用方法:
- (NSCalendar *)calendarViewUsesCalendar:(ABCCalendarView *)calendarView;
但是,查看 Cocoa 框架中这种情况的实际示例,我可以看到实际使用的约定似乎更喜欢将 class 名称放在方法名称的末尾这个案例。例如:
// UITableViewDataSource has the method:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
// UITableViewDelegate has:
- (NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView
// UIScrollView has:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
如果我遵循这个建议我应该调用我的方法的约定:
- (NSCalendar *)calendarForCalendarView:(ABCCalendarView *)calendarView;
为什么 Apple 的指导方针与他们的实际做法之间似乎存在差异?在创建基本上只是设置调用对象的 属性 的委托方法时应该遵循什么约定?
我认为 rules document 内部有点不一致。有两个 "General Rules" 其中状态:
If the method returns an attribute of the receiver, name the method after the attribute.
和
Make the word before the argument describe the argument.
这些以及 SDK 名称中的先例都支持您的第二种表述:
- (NSCalendar *)calendarForCalendarView:(ABCCalendarView *)calendarView;
我认为 SDK 名称中有充分的证据表明您在委托部分找到的规则应该重述。正如你指出的那样,它说
Start the name by identifying the class of the object that’s sending the message
但我认为它应该说:"Send an instance of the caller as the first parameter"。这是一半命名规则,一半设计模式。这样,那些返回委托名称的单个参数、属性可以采用 returnTypeSenderType:
的形式(实际上是这样),而不会 运行 与文档的其他部分发生冲突。