Google objective-c Coding style (1) Stay white and format

Source: Internet
Author: User

Empty lines are not required before and after @interface,@implementation , and @end . If you declare an instance variable in the @interface , you must have an empty line after the closing parenthesis } .

Space vs. tab

Use only spaces, and indent two spaces at a time.

We use spaces to indent. Do not use tabs in your code. You should set the editor to automatically replace tabs with spaces.

Line width

Try to keep your code within 80 columns.

By setting up Xcode > Preferences > Text Editing > Show pageGuide, you can make it easier to detect cross-border.

Method declarations and definitions

Tip

    • A space must be used between the/+ and return types, and only the parameters in the argument list can have spaces between them.

The method should look like this:

(void)dosomethingwithstring:(*)... }

The space before the asterisk is optional. When writing new code, keep it consistent with the previous code.

If a row has a lot of parameters, a better way is to separate each parameter into a single row. If multiple rows are used, the colon before each parameter is aligned.

(void)dosomethingwith:(*)rect:(nsrect)interval: (float)... }

When the first keyword is shorter than the other, ensure that the next line is indented at least 4 spaces. This allows you to align your keywords vertically, rather than using colons:

(void) Short:(*)Longkeyword:(nsrect)Evenlongerkeyword:(  float)... }

Method invocation

Tip

The method invocation should be kept as consistent as the method declaration format. When the style of the format has multiple choices, the new code will be consistent with the existing code.

All parameters should be on the same line when called:

[dofoowith:name:error:arg3];       

or one parameter per line, aligned with a colon:

[dofoowith:arg1               Name:error:arg3];      

A method definition is like a method declaration, where the length of a keyword is not sufficient to align with a colon, and the next line is indented in four spaces.

[Short:arg1    longkeyword:evenlongerkeyword:arg3];

@publicAnd@private

Tip

@public and @private access modifiers should be indented with a space.

Very similar to public , private , and protected in C + +.

@public ...   }@end  


Abnormal

Tip

Each @ tag should have a separate line, with a space between @ and {} , and a space between @catch and the declaration of the exception object being caught.

If you decide to use the objective-c exception, then press the following format. But you'd better take a look at avoiding throwing exceptions to understand why you shouldn't use exceptions.

{  foo();} (*exBar(ex);} Baz();}             
Protocol Name

Tip

There cannot be any spaces between the type identifier and the protocol name within the angle brackets.

This rule applies to class declarations, instance variables, and method declarations. For example:

NSObject<nswindowdelegateID<myfancydelegatedelegate_;} (void)setdelegate:(ID<myfancydelegate>)  Adelegate;  @end                  
Block (closed package)

Tip

Block is suitable for creating callback methods in Target/selector mode because it makes the code easier to read. The code in the block should be indented in 4 spaces.

Depending on the length of the block, the following are reasonable style guidelines:

    • If a line can finish writing blocks, there is no need to wrap.
    • If you have to break the line, the closing brackets should be aligned with the first character of the block declaration.
    • The code within the block must be indented by 4 spaces.
    • If the block is too long, such as more than 20 rows, it is recommended to define it as a local variable and then use that variable.
    • If the block has no parameters, no spaces are required between^{ . If there is a parameter,^ ( no spaces between, but ) { there must be a space between.}
    • The block is allowed to indent by two spaces, but only if the indentation style is consistent with the other code in the project.
The entire block fits on one line.[OperationSetcompletionblock:^{[SelfOnoperationdone];}];The block can be put to a new line, indented four spaces, with theClosing brace aligned with the first character of the "on which"Block was declared.[OperationSetcompletionblock:^{[Self.DelegateNewdataavailable];}];Using a block with a C API follows the same alignment and spacingRules as with objective-c.Dispatch_async(Fileioqueue_,^{NSString*Path=[SelfSessionfilepath];If(Path){// ...}});An example where the parameter wraps and the block declaration fitsOn the same line. Note the spacing of |^ (Sessionwindow *window) {|Compared to |^{| above.[[SessionserviceSharedservice]Loadwindowwithcompletionblock:^(Sessionwindow*Window){If(Window){[SelfWindowdidload:Window];}Else{[SelfErrorloadingwindow];}}];An example where the parameter wraps and the block declaration doesNot fit on the same line as the name.[[SessionserviceSharedservice]Loadwindowwithcompletionblock:^(Sessionwindow*Window){If(Window){[self windowdidload:window; } else {[self  Errorloadingwindow; } }]; //Large blocks can be declared out-of-line. void  (^largeblock) ( void = ^{//... }; [operationqueue_ addoperationwithblock: largeblock];             




Google objective-c Coding style (1) Stay white and format

Related Article

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.