Coding guidelines for cocoa

Source: Internet
Author: User

Refer:

[1] Apple official

[2] Three programming naming rules (Hungary, small hump, and big hump)

The following is my personal reading notes for coding guidelines for cocoa. It mainly lists several items that are easy to violate. For details, see reference 1.

1. clarity and consistency

1) Apple generally uses the small camper name method, that is, the first letter of the first word is lowercase, and the first letter of other words is capitalized, for example, codename.

2) Naming should be concise and clear, without ambiguity, and abbreviation should not be used, and full spelling should be applied.

3) Naming should be consistent. For example, methods with the same functions in multiple classes should have the same name, so that they can be used at will.

2. prefix

1) It is well known that ns, IB, and AB are Apple's own prefixes. Each project should have a unique prefix during user development, which can prevent name conflicts with Apple system libraries and third parties. In objective-C, there is no namespace concept. All prefixes are even more important.

2) use a prefix in a name with external links, such as a class name, protocol name, global function, constant, or struct. A prefix is not required for a name without external connectivity, for example, the method name and category data member (except for Apple's own ).

3) Apple adds "_", this prevents users from accidentally overwriting the member variables and member methods in the system base class when inheriting the system class.

4) The default class member is the @ protected attribute, which can be accessed by subclass and class methods.

3. protocol naming

1) it is not associated with a specific class, but only combines a group of related operations. The protocol name should be irrelevant to the specific class.

2) protocol for a set of operations associated with a specific class. The protocol name should be the same as the class name, for example, nsobject protocol.

4. Name the header file, which is generally the same as the class name. If there are several related functions and types, use the relevant name.

5. Class extension name

1) Extend an API for a class in an existing framework: If you want to declare a category class method for a class in another framework in one framework, the header file is named as follows: the original class name + "additions ". For example, nsbundleadditions. h In Application Kit

6. Delegate Method

1) The name starts with the class name for sending the message. The prefix is omitted and the first letter in lower case is used,

2) The first parameter is the message sending object, except when there is only one parameter, for example: Notification

-(Void) windowdidchangescreen :( nsnotification
*) Notification;

3) indicates whether the notification delegate object is about to occur or has been used with Will, did, and whether it can be executed with sho'd

7. method parameters

1) do not use one, two... to name parameters,

2) generally, and is not used, because it is difficult to use multiple and parameters.

8. The bitmask constant can use an unnamed enumeration. For example:

Enum {

Nsborderlesswindowmask = 0,

Nstitledwindowmask = 1 <0,

Nsclosablewindowmask = 1 <1,

Nsminiaturizablewindowmask = 1 <2,

Nsresizablewindowmask = 1 <3

};

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.