Objective-C coding specifications

Source: Internet
Author: User

DiscussionObjective-C encoding SpecificationIs the content to be introduced in this article.Objective-COfCode Specification.Code SpecificationI believe programmers all know that this is the most important thing to pay attention.

Format code

Position of the pointer "*"▪For example, NSString * varName;

Space VS tabs▪Only spaces are allowed. The editor is set to 1 TAB = 2 Characters indented.

Length of each line

Each line cannot exceed 100 characters

With a 15-inch Macbook Pro, each line can contain 100 characters to maximize simultaneous editor and iPhone Simulator

Google's 80-character standard is a little small, which leads to too frequent line breaks (Objectve-C code is generally very long)

Use "Xcode => Preferences => TextEditing => check Show Page Guide/Input

100 => OK "to set the description and definition of the alert Method

Leave a space between-OR + and the return value, and leave no space between the method name and the first parameter. For example:

 
 
  1. - (void)doSomethingWithString:(NSString *)theString {  
  2. ...  

When the parameter is too long, each parameter occupies one line and is aligned with a colon. For example:

 
 
  1. - (void)doSomethingWith:(GTMFoo *)theFoo  
  2. }  
  3. ...  
  4. rect:(NSRect)theRect interval:(float)theInterval { 

If the method name is shorter than the parameter name, each parameter occupies a line and is Indented by at least four characters and vertically aligned (rather than colons ). For example:

 
 
  1. - (void)short:(GTMFoo *)theFoo  
  2. }  
  3. longKeyword:(NSRect)theRect  
  4. evenLongerKeyword:(float)theInterval { ... 

Call Method▪Calling Methods follows the habit of declaring methods. Exception: If the given source file has followed a certain habit, continue to follow that habit.▪All parameters should be in the same line, or each parameter occupies one line and is aligned with a colon. For example:

 
 
  1. [myObject doFooWith:arg1 name:arg2 error:arg3]; 

Or

 
 
  1. [myObject doFooWith:arg1 name:arg2  
  2. error:arg3]; 

Like method declaration, if you cannot use colons for alignment, each parameter has a line, indentation of 4 characters, and vertical alignment (rather than colon alignment ). For example:

 
 
  1. [myObj short:arg1  
  2. longKeyword:arg2  
  3. evenLongerKeyword:arg3]; 

@ Public and @ private▪@ Public and @ private use a separate line with a indentation of 1 Character

No spaces are left between the Protocals type identifier, proxy name, and angle brackets. This rule also applies to: class declaration, instance variable, and method declaration. For example:

 
 
  1. @interface MyProtocoledClass : NSObject<NSWindowDelegate> { @private  
  2. id<MyFancyDelegate> _delegate;  
  3. } - (void)setDelegate:(id<MyFancyDelegate>)aDelegate; @end 

If the class declaration contains multiple protocal, each protocal occupies one line and is Indented by two characters. For example, @ interface CustomViewController: ViewController <

 
 
  1. AbcDelegate,  
  2. DefDelegate >{  

Name of the category class

The first letter of the Class name (and its category name and protocal name) is capitalized.

In code for specific applications, class names should avoid prefix. Each class uses the same prefix to affect readability.

Prefix is recommended in multi-application-oriented code. Example: GTMSendMessage Category Name

Method name to be improved

The method name starts with lowercase letters and uses uppercase letters to separate words. The method parameters use the same rules.▪The method name + parameter should be read as much as possible like a sentence (for example :).

Here, let's look at Apple's naming rules for methods.▪The method name and variable name of getter must be the same. You cannot use the "get" prefix. For example:

 
 
  1. -(Id) getDelegate; // disable
  2. -(Id) delegate; // opposite

This rule is only applicable to Objective-C code and C ++ code.

Variable name▪The full name of the variable name should be easy to use, and the first letter is lowercase, and the word is separated in the form of an upper letter▪The member variables use "_" as the prefix (for example, "NSString * _ varName ;".

Although this conflicts with Apple's standard (using "_" as the suffix), "_" is still used as the prefix for the following reasons.▪Using "_" as the prefix makes it easier to differentiate "attributes in the IDE with the code auto-complementing function.

(Self. userInfo) "and" member variable (_ userInfo )"▪Constants (# define, enums, const, etc.) Use lowercase "k" as the prefix, and the first letter is capitalized to separate words. For example:

 
 
  1. kInvalidHandle 

Annotation pending

Cocoa and Objective-C Rules use @ private as the alias member variable. For example:

 
 
  1. @ Interface MyClass: NSObject {@ private
  2. Id _ myInstanceVariable;
  3. } // Public accessors, setter takes ownership-(id) myInstanceVariable;-(void) setMyInstanceVariable :( id) theVar; @ end
  4. Indentify Designated Initializer▪To be improved
  5. Override Desingated Initializer

Summary:Objective-C encoding SpecificationI hope this article will help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.