xcode Objective C 方法命名约定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8410602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective C method naming convention
提问by Ryan
I am currently using the following conventions
我目前使用以下约定
- (id) initWithName:(NSString *) name;
+ (NSString *) aliasForName:(NSString *) name
- (void) method
- (void) methodWithApple:(NSString *) apple andOrange:(NSString *) orange
andMango:(NSString *) mango
- (void) statusWasChanged:(id)sender
Do you have a better style for the above methods?
以上方法你有更好的风格吗?
Thanks
谢谢
回答by Sam
Coding Guidelines for Cocoais a great resource for answering any naming convention questions. My answer is as much as possible based off of this.
Cocoa 编码指南是回答任何命名约定问题的重要资源。我的回答是尽可能基于此。
Init Method
初始化方法
The init method looks good.
init 方法看起来不错。
- (id) initWithName:(NSString *) name;
Class Method
类方法
The class method looks good.
类方法看起来不错。
+ (NSString *) aliasForName:(NSString *) name
Class methods can also be used to instantiate an instance of an object. In this instance, Apple's API's generally have the method start with the name of the class like UIButton
's buttonWithType:
method that has the signature:
类方法还可用于实例化对象的实例。在这种情况下,Apple 的 API 的方法通常以类的名称开头,例如具有签名UIButton
的buttonWithType:
方法:
+ (id)buttonWithType:(UIButtonType)buttonType
Instance Methods
实例方法
Good resource for coding conventions for methods can be found under General Rules.
可以在通用规则下找到有关方法编码约定的良好资源。
The following method should drop the "and"
s:
以下方法应该删除"and"
s:
- (void) methodWithApple:(NSString *) apple andOrange:(NSString *) orange
andMango:(NSString *) mango // BAD
Don't use “and” to link keywords that are attributes of the receiver.
- (int)runModalForDirectory:(NSString *)path file:(NSString *) name types:(NSArray *)fileTypes;
right
- (int)runModalForDirectory:(NSString *)path andFile:(NSString *)name andTypes:(NSArray *)fileTypes;
wrong
不要使用“and”来链接作为接收者属性的关键字。
- (int)runModalForDirectory:(NSString *)path file:(NSString *) name types:(NSArray *)fileTypes;
对
- (int)runModalForDirectory:(NSString *)path andFile:(NSString *)name andTypes:(NSArray *)fileTypes;
错误的
The signature should look more like the following:
签名应该更像以下内容:
- (void) methodWithApple:(NSString*)apple orange:(NSString*)orange
mango:(NSString*)mango // GOOD
Delegate Methods
委托方法
Lastly, I think there are a couple improvements that could be made on what appears to be a delegate method:
最后,我认为可以对看似委托的方法进行一些改进:
- (void) statusWasChanged:(id)sender // Not horrible, but not ideal
First improvement is to add the class name to the method.
第一个改进是将类名添加到方法中。
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;
Second improvement is to use "DidChange"
instead of "WasChanged"
.
第二个改进是使用"DidChange"
代替"WasChanged"
.
Use “did” or “will” for methods that are invoked to notify the delegate that something has happened or is about to happen.
将“did”或“will”用于被调用以通知委托某事已经发生或即将发生的方法。
- (void)browserDidScroll:(NSBrowser *)sender;
- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window;
Third improvement is strongly casting the sender parameter. I don't have documentation to support this, however all examples provided in the examples exude this behavior. Notice the (NSBrowser*)sender
and (NSWindow*)window
in the above code sample taken straight from the apple docs.
第三个改进是强烈地投射发送者参数。我没有支持这一点的文档,但是示例中提供的所有示例都散发出这种行为。注意上面直接取自苹果文档的代码示例中的(NSBrowser*)sender
和(NSWindow*)window
。
With this in mind, the delegate method should look more like:
考虑到这一点,委托方法应该看起来更像:
- (void) senderClassNameStatusDidChange:(SenderClassName*)sender // Good
If the sender were a Person object it would look like:
如果发件人是一个 Person 对象,它看起来像:
- (void) personStatusDidChange:(Person*)sender // Good
A word of caution is that you shouldn't always use "did" in delegate methods.
需要注意的是,您不应该总是在委托方法中使用“did”。
Although you can use “did” or “will” for methods that are invoked to ask the delegate to do something on behalf of another object, “should” is preferred.
尽管您可以将“did”或“will”用于要求委托代表另一个对象做某事的方法,但首选“should”。
- (BOOL)windowShouldClose:(id)sender;
回答by Kendall Helmstetter Gelner
I would say the only one I'm not sure about is:
我会说唯一一个我不确定的是:
- (void) methodWithApple:(NSString *) apple andOrange:(NSString *) orange
andMango:(NSString *) mango
It seems like the "and" in the last two arguments is either unnecessary or should be replaced by a verb. I think a good name really depends a lot on the context of the call, and what will be done with the parameters sent in.
似乎最后两个参数中的“and”要么是不必要的,要么应该用动词代替。我认为一个好的名字在很大程度上取决于调用的上下文,以及传入的参数会做什么。
回答by Thomas Joulin
- (id) initWithName:(NSString *) name;
Any method starting with init
is understood by the framework as a method returning an retained object (difference between initWithObjectsAndKeys
and dictionaryWithObjectsAndKeys
for example). So you should use this convention with that in mind, especially when using ARC.
init
框架将任何以 开头的方法理解为返回保留对象的方法(例如initWithObjectsAndKeys
和之间的差异dictionaryWithObjectsAndKeys
)。因此,您应该牢记这一点来使用此约定,尤其是在使用 ARC 时。
+ (NSString *) aliasForName:(NSString *) name
Using the same convention, this type of method would return autoreleased objects (static methods or not)
使用相同的约定,这种类型的方法将返回自动释放的对象(静态方法与否)
- (void) method
I would say that if there is only one word, and this word is a noun, it should be the getter of a property (like view
, superview
...). Otherwise if it's a verb, why not also add the object it refers to ? Like closeModalViewController
我会说,如果只有一个词,并且这个词是名词,它应该是属性的吸气剂(例如view
,superview
...)。否则,如果它是动词,为什么不添加它所指的对象呢?喜欢closeModalViewController
- (void) methodWithApple:(NSString *) apple andOrange:(NSString *) orange
andMango:(NSString *) mango
I would drop the and
indeed.
我and
确实会放弃。
- (void) statusWasChanged:(id)sender
A more Apple-y way would be statusDidChange:
更像苹果的方式是 statusDidChange:
回答by Jeff Wolski
Those are good naming conventions except for the 'and'. I tend to look at the Google Style Guide'.
除了“和”之外,这些都是很好的命名约定。我倾向于查看Google Style Guide'。