Objective-c code and naming conventions in iOS

Source: Internet
Author: User

To be an iOS development engineer, code naming is unprofessional, because the code that iOS development engineers learn will show a lot of people, and in order to make the code clear and concise, easy to read and understand, it will be consistent with certain code specifications, Objective-c.

Simply summarize what you are currently exposed to:

1, the code line is the maximum of 100 columns (C + + is 80)

2, when declaring a class or method, pay attention to the use of space, the argument is too many lines to maintain alignment, the call method is also the case, the parameters are written in one line or a newline colon alignment.

3. Naming rules

The first letter of the class name, the method of the first letter lowercase, the method of the first letter lowercase, while trying to make the name of the method read like a sentence, to convey the meaning of the method, and not prefix "get" before the value method

-(void) Choosecardatindex: (Nsuinteger) index;

Variable name starts with lowercase letters

Int:chosenbuttonindex=[self.cardbuttons Indexofobject:sender];

Constants begin with a lowercase letter K, followed by the first letter capitalized

Static Const nsstring* Klnvecservicestring

4. About annotations

Note is important, but in addition to the beginning of the copyright notice, as far as possible to write the code as a document, let others directly read the code to know the meaning of the code, do not worry about the name is too long, I believe Xcode's prompt function.

5. Instance variables should be declared in the implementation file. m or in the @property form in the. h file, be sure to declare it directly in the. h file, plus @priavte, and in addition, use @private, @public, preceded by an indent space.

6. As far as possible to ensure the simplicity of the. h file, you can not expose the API is not public, write in the implementation file.

7.Xcode support objective-c/c/c++, so when referring to header file: #import ojbective-c/objective-c++ header file (objective-c++ is objective-c and C + + files), #include A/C + + header file.

8. Write delegate when the type should be weak weak reference, in order to avoid circular reference, when the delegate object does not exist, we write the delegate there is no meaning of the natural need to destroy.

@property (Nonatomic,weak) id<secondviewcontrollerdelegate> delegate;

9. Instance variable declaration when the variable name is preceded by an underscore "_", the local variable is not added.

10. When using block, the contents of four spaces are indented, "^" with parameters, there is a space between the parameter and "{" Indentation

11. It is recommended to use "#pragma mark" for easy reading of code

For more detailed reference two specifications, Coding guidelines for Cocoa also lists detailed naming requirements.

Objective-c code and naming conventions in iOS

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.