Objective-C Code specification (Code Style)

Source: Internet
Author: User

The code we wrote will be shown to many people. In order to make the code clear, concise, and easy to read and understand, it will all comply with certain code specifications, as does Objective-C.

Main reference specifications:

1. Google Objective-C Style Guide

2. Coding GuidelinesCocoa

Summary:

1. The maximum code level is 100 columns (80 for C ++)

2. When declaring a class or method, pay attention to the use of spaces. If there are too many parameters, line breaks can be kept aligned,

This is also true when a method is called. parameters are written in one line or lined with colons,

3. Naming rules

The first letter of the class name is in upper case, the first letter of the method is in lower case, and the first letter of the parameter in the method is in lower case. At the same time, try to make the method name read like a sentence to convey the meaning of the method, do not add the prefix "get" before the value method"

Variable name starts with a lowercase letter

A constant starts with a lowercase letter k, followed by an uppercase letter

4. Notes

Note is very important, but apart from the copyright statement at the beginning, try to write the code like a document, so that others can understand the meaning of the Code directly. Do not worry that the name is too long when writing the code, trust the prompt function of Xcode.

5. instance variables should be in the implementation file. m is declared or in the form of @ property in. the statement in the H file must be directly in. add @ priavte to the H file Declaration. In addition, use @ private and @ public. an indent space is required first.

6. Try to ensure the simplicity of the. h file. Do not expose the API without disclosing it. Simply write it in the implementation file.

7. xcode supports Objective-C/C ++, so when you reference the header file: # import Ojbective-C/Objective-C ++ header file (Objective-C ++ is a mix of Objective-C and C ++ files), # include C/C ++ header files.

8. when writing delegate, the type should be weak reference to avoid circular references. When the delegate object does not exist, the delegate we write will have no meaning and naturally needs to be destroyed, for more information about weak and strong, see the previous article.

9. When the instance variable is declared, the variable name must be prefixed with an underscore "_". You do not need to add a local variable.

10. When a Block is used, the content is Indented by four spaces. When "^" is followed by a parameter, there is a space indent between the parameter and "{".

11. We recommend that you use "# pragma mark" to read the code.

For other details, refer to the two specifications, Coding GuidelinesCocoa Detailed naming requirements are also listed.

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.