objc編碼規範

來源:互聯網
上載者:User
常量執行個體以k開頭。雙底線開頭和結尾的macro是編譯器保留的
naming methods:
  • Don’t use “and” to link keywords that are attributes of the receiver.
  • If the method describes two separate actions, use “and” to link them.
  • Use “get” only for methods that return objects and values indirectly. You should use this form for methods only when multiple items need to be returned.
delegate methods:
  • Start the name by identifying the class of the object that’s sending the message
// 下面就是TableView類在delegate object中的回呼函數,sending是TableView類

- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(int)row;

  • Use “did” or “will” for methods that are invoked to notify the delegate that something has happened or is about to happen.
  • 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.

Collection Methods

For objects that manage a collection of objects (each called an element of that collection),
the convention is to have methods of the form:

- (void)addElement:(elementType)anObj;

- (void)removeElement:(elementType)anObj; - (NSArray *)elements;

The following are some qualifications and refinements to this guideline:

  • If the collection is truly unordered, return an NSSet object rather than an NSArray object.
  • If it’s important to insert elements into a specific location in the collection, use methods similar to the following instead of or in addition to the ones above:
- (void)insertLayoutManager:(NSLayoutManager *)obj atIndex:(int)index; 
- (void)removeLayoutManagerAtIndex:(int)index;

There are a couple of implementation details to keep in mind with collection methods:
  • These methods typically imply ownership of the inserted objects, so the code that adds or inserts them must retain them, and the code that removes
    them must also release them.
  • If the inserted objects need to have a pointer back to the main object, you do this (typically) with a set... method
    that sets the back pointer but does not retain.

Names of most private methods in the Cocoa frameworks have an underscore prefix (for example, _fooData )
to mark them as private. From this fact follow two recommendations.

  • Don’t use the underscore character as a prefix for your private methods. Apple reserves this convention.
  • If you are subclassing a large Cocoa framework class (such as NSView or UIView) and
    you want to be absolutely sure that your private methods have names different from those in the superclass, you can add your own prefix to your private methods. The prefix should be as unique as possible, perhaps one based on your company or project and of
    the form "XX_". So if your project is called Byte Flogger, the prefix might be BF_addObject:

Function Name:

Functions have some general naming rules that you should follow:

  • Function names are formed like method names, but with a couple exceptions: 
    • They start with the same prefix that you use for classes and constants.
    • The first letter of the word after the prefix is capitalized.
  • Most function names start with verbs that describe the effect the function has:
      NSHighlightRect 

      NSDeallocateObject
Property Name:

  • If the name of a declared property is expressed as an adjective, however, the property name omits the “is” prefix but specifies the conventional name for the get accessor, for example:

     @property (assign, getter=isEditable) BOOL editable;
Enumerated constants

typedef enum _NSMatrixMode 
{    NSRadioModeMatrix    NSHighlightModeMatrix    NSListModeMatrix    NSTrackModeMatrix 
} NSMatrixMode;


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.