什么时候应该在Objective-C方法名称中使用“和”?

时间:2022-04-08 19:51:02

The Apple style guides for naming methods say both "Don’t use “and” to link keywords that are attributes of the receiver." and "If the method describes two separate actions, use “and” to link them." In many cases, neither of these conditions apply. For example, let's say I have a function to do some work and call a selector on a target when it is done. Should this be named

用于命名方法的Apple风格指南表示“不使用”和“链接作为接收者属性的关键字”。并且“如果该方法描述了两个单独的操作,请使用”和“链接它们。”在许多情况下,这些条件都不适用。例如,假设我有一个函数来做一些工作,并在完成后调用目标上的选择器。应该这个名字

- (void)findObjectsInBackgroundWithTarget:(id)target andSelector:(SEL)selector;

or should this be named

或者应该这个名字

- (void)findObjectsInBackgroundWithTarget:(id)target selector:(SEL)selector;

In that example, the selector and target are related, but not through being attributes of the receiver. A similar example is to retrieve an object with particular attributes, but the object is not the receiver of the method. Should this function be

在该示例中,选择器和目标是相关的,但不是通过接收器的属性。类似的示例是检索具有特定属性的对象,但该对象不是该方法的接收者。应该这个功能

+ (Thing *)getThingWithName:(NSString *)name andId:(NSString *)thingId;

or should this be named

或者应该这个名字

+ (Thing *)getThingWithName:(NSString *)name id:(NSString *)thingId;

I know this might seem like a minor thing, but it's nice to use the style everyone expects.

我知道这可能看起来很小,但是使用每个人都期望的风格会很好。

1 个解决方案

#1


7  

According to what Apple says, you should use "and" only if the name of your method describes the action the method does, i.e. -(void)doThisAndThat, so in both of your examples you should not use "and" in methods' names.

根据Apple的说法,只有当你的方法名称描述了方法所做的动作时才应该使用“和”,即 - (void)doThisAndThat,所以在你的两个例子中你不应该在方法的名字中使用“和” 。

#1


7  

According to what Apple says, you should use "and" only if the name of your method describes the action the method does, i.e. -(void)doThisAndThat, so in both of your examples you should not use "and" in methods' names.

根据Apple的说法,只有当你的方法名称描述了方法所做的动作时才应该使用“和”,即 - (void)doThisAndThat,所以在你的两个例子中你不应该在方法的名字中使用“和” 。