Objective-C code specification (6.1.12 Wang Bin) and objectivec code specification

Source: Internet
Author: User

Objective-C code specification (6.1.12 Wang Bin) and objectivec code specification

 

First, official code specifications

Objective-C coding specifications, content from Apple and Google's document translation, their coding experience and summary of other materials.

Summary

Objective-C is an object-oriented dynamic programming language, mainly used to write iOS and Mac applications. Apple has summarized the Objective-C encoding specifications:

  • Apple Coding Guidelines for Cocoa

This article mainly integrates the translation of the above documents, the author's own programming experience and other relevant materials, and summarizes a general coding specification for the company.

Website: http://www.cocoachina.com/ios/20150508/11780.html

 

Second, some experiences

Class naming principles

Take "NSMutableArray" as an example.

NS --- Prefix my understanding is to indicate the "package" to which the class belongs. Here, NS indicates the base class library of Cocoa and the Project Name of the company name. (NS is short for NextStep, and its origins are not described in detail.-) the meaning of the prefix lies in the intuitive division of the class's ownership and scope. For example, the prefix of Cocos2d is CC, and box2d contains B2, UserInterface-> UI, CoreFoundation-> CF, CoreGraphics-> CG. If I can write an open-source architecture or module, I should use "SY" or "Sunny :)
MutableArray --- class name, which briefly and clearly describes the content represented by the class. The first letter is in upper case, indicating the hump. It is also worth noting that NSMutableArray is an extension subclass of NSArray and is essentially an Array. Therefore, the identification words "Array" of the parent class should be kept ", however, NSObject to NSArray should not be named as NSArrayObject, which also avoids semantic overlap.

 

Naming rules for member variables and attribute accessors

This also made me struggle for a long time. I have never understood why many member variables in the Code contain underscores (_), but the attribute access method @ property declaration is not underlined, @ synthesize makes the two names equal.

@interface SunnyTest : NSObject {    NSArray * _array;  }@property (nonatomic,retain) NSArray * array;@end

 

@implementation SunnyTest@synthesize array = _array;@end

The reason for doing so is that it does not expose the instance's member variables. The outside world can only use the access name declared by @ property to access the member variables, so it is distinguished by a slide line.

However, when a class method accesses a member variable, it directly uses an underlined name. In my opinion, do not use self. xxxxx is used, because the accessor is used externally, internal use may cause unnecessary errors, such:

Self. array = [[NSArray alloc] init]; // Memory leakage

This will cause memory leakage, because [[NSArray alloc] init] generates the NSArray object and assigns it to self. array, because the array access method contains retain, this will change retainCount to 2, but actually it should be 1, memory leakage and difficult to find.

This method is also officially recommended by Apple.

_array = [[NSArray alloc] init];

 

Method naming rules

 A standard method should read like a complete sentence. After reading it, you will know the role of the function. The execution method should start with a verb and start with a lowercase letter. The return method should start with the returned content, but do not add get before.

 

- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
+ (id)arrayWithArray:(NSArray *)array;

If there are parameters, the function name should be used as the prompt information for the first parameter. If there are multiple parameters, there should also be prompt information before the parameter (and is generally not required)

Some classic operations should use agreed verbs, such as initWith, insert, remove, replace, and add.

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.