OBJECTIVE-C Code specification (2016.1.12 Wang Bin)

Source: Internet
Author: User





The first word Official Code specification


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



Profile



Objective-c is an object-oriented, dynamic programming language used primarily for writing IOS and MAC applications. Apple has a good summary of the coding specifications for Objective-c:


    • Apple Coding Guidelines for Cocoa


This paper mainly integrates the translation of the above documents, the author's own programming experience and other related materials, for the company summed up a general code specification.



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





The second words own some experience of experience


Class naming principles



Take "Nsmutablearray" for a while.



NS---prefix (Prefix) My understanding is that the class belongs to the "package", where NS represents the cocoa base class library, the name of the company name of the project or something. (NS is the abbreviation of NeXTSTEP Company, the origin of it does not elaborate--) the meaning of the prefix is more intuitive division of the class's ownership and scope. Like the prefix in cocos2d is cc,box2d inside is B2,USERINTERFACE->UI,COREFOUNDATION->CF,COREGRAPHICS->CG. If I can also write an open source architecture or module, I should take "SY" or "Sunny":)
Mutablearray---Class name, a short, unambiguous description of what the class represents. First letter capitalized, hump marked. It is also worth noting that since Nsmutablearray is an extended subclass of Nsarray, which is essentially described as an array, it should retain the recognition "array" of the parent class, but nsobject to Nsarray should not be named Nsarrayobject , but also avoids the overlap of semantics.






member variables and property accessors (Accessor) naming principles



This is also let me struggle for a long time, has not understood why a lot of code inside the class member variable appears underscore "_", and the property access method @property declaration without underlining, when implemented @synthesize also makes two names equal.


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

 

@implementation SunnyTest
@synthesize array = _array;
@end


The reason for this is very simple, is not to expose the member variables of the instance, the outside world can only use the access name of the @property declaration to access the member variables, so it is used to distinguish the downward line.



However, when accessing member variables in a class method, the underlined name is used directly, and the individual thinks not to use SELF.XXXXX, because the use of accessors is inherently external and may cause unnecessary errors in internal use, such as:


Self.array = [[Nsarray alloc] init]; Memory leaks


This will cause a memory leak because [[Nsarray alloc] init] After generating the Nsarray object is assigned to Self.array, because the array's access method contains retain, which causes Retaincount to become 2, and actually should be 1, memory leaks and difficult to find.



It is the official recommendation of Apple to change the wording.


_array = [[Nsarray alloc] init];





Method naming rules



A canonical method should read like a complete sentence, and then read the function. The execution method should begin with a verb, the lowercase letter begins, and the return method should start with the returned content, but do not precede the get.





-(void) Replaceobjectatindex: (nsuinteger) index Withobject: (ID) anobject;
+ (ID) Arraywitharray: (Nsarray *) array;


If there are parameters, the function name should be the first parameter of the prompt information, if there are more than one parameter, before the parameter should also have a hint information (generally do not have to add and)



Some classic operations should use the agreed verbs, such as initwith,insert,remove,replace,add and so on.



Objective-c Code specification (2016.1.12 Wang Bin)


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.