Programming with Objective-C方法命名规范

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Conventions/Conventions.html

Method Names Should Be Expressive and Unique Within a Class

Once you’ve chosen a unique name for a class, the methods that you declare need only be unique within that class. It’s common to use the same name as a method in another class, for example, either to override a superclass method, or take advantage of polymorphism. Methods that perform the same task in multiple classes should have the same name, return type and parameter types.
Method names do not have a prefix, and should start with a lowercase letter; camel case is used again for multiple words, like these examples from the NSString
class:
length

characterAtIndex:

lengthOfBytesUsingEncoding:

If a method takes one or more arguments, the name of the method should indicate each parameter:
substringFromIndex:

writeToURL:atomically:encoding:error:

enumerateSubstringsInRange:options:usingBlock:

The first portion of the method name should indicate the primary intent or result of calling the method. If a method returns a value, for example, the first word normally indicates what will be returned, like the length
, character...
and substring...
methods shown above. Multiple words are used if you need to indicate something important about the return value, as with the mutableCopy
, capitalizedString
or lastPathComponent
methods from the NSString
class. If a method performs an action, such as writing to disk or enumerating the contents, the first word should indicate that action, as shown by the write...
and enumerate...
methods.
If a method includes an error pointer parameter to be set if an error occurred, this should be the last parameter to the method. If a method takes a block, the block parameter should be the last parameter in order to make any method invocations as readable as possible when specifying a block inline. For the same reason, it’s best to avoid methods that take multiple block arguments, wherever possible.
It’s also important to aim for clear but concise method names. Clarity doesn’t necessarily mean verbosity but brevity doesn’t necessarily result in clarity, so it’s best to aim for a happy medium:

stringAfterFindingAndReplacingAllOccurrencesOfThisString:withThisString:

Too verbose

strReplacingStr:str:

Too concise

stringByReplacingOccurrencesOfString:withString:

Just right

You should avoid abbreviating words in method names unless you are sure that the abbreviation is well known across multiple languages and cultures. A list of common abbreviations is given in Acceptable Abbreviations and Acronyms.
Always Use a Prefix for Method Names in Categories on Framework Classes
When using a category to add methods to an existing framework class, you should include a prefix on the method name to avoid clashes, as described in Avoid Category Method Name Clashes.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容