iOS Development Code Specification

Source: Internet
Author: User

1. Unified Requirements for naming 1>
    • The meaning is clear, as far as possible do not need to comment also can understand its role, if cannot do, add comment

    • Use full name, not applicable abbreviation

Name of the 2> class
    • Big Hump-type naming: Each word is capitalized with the first letter

Example: Mfhomepageviewcontroller

    • Suffix requirements

Viewcontroller: Use Viewcontroller to do suffix

Example: Mfhomeviewcontroller

View: Using view as a suffix

Example: Mfalertview

Uitablecell: Use cell to make suffix

Example: Mfnewscell

Protocol: Use delegate or datasource as suffix

Example: Uitableviewdelegate

UI controls in turn

3> Private variables
    • Small Hump-type naming: The first word starts with a lowercase letter, the first letter of the following words is all capitalized

Example: FirstName, LastName

    • Start with _, first letter lowercase

Example: NSString * _someprivatevariable

    • Private variables are declared in the. m file
4> Property Variables
    • Small Hump-type naming

Example:///Comment

@property (nonatomic, copy) NSString *username;

    • Prohibit the use of synthesize keywords
5> macro Naming
    • All uppercase, separated by _ between words. [Without parameters]

Example: #define THIS_IS_AN_MACRO @ "This_is_an_macro"

    • Start with the letter K, followed by the big hump named. [Without parameters]

Example: #define Kwidth self.frame.size.width

    • Small hump named. [With parameters]

#define GETIMAGEURL (URL) [Nsurl urlwithstring:[nsstring stringwithformat:@ "%@%@", Kbaseurl,url]]

6> Enum
    • The name of the enum type is consistent with the naming rules of the class

    • The naming of enumerated content in an enum needs to start with the enum type name

Example:

1 typedef ns_enum (Nsinteger, Afnetworkreachabilitystatus) {2     Afnetworkreachabilitystatusunknown = -1,3     afnetworkreachabilitystatusnotreachable = 0,4     Afnetworkreachabilitystatusreachableviawwan = 1,5     Afnetworkreachabilitystatusreachableviawifi = +     };
7> delegate naming
    • The instance of the class must be one of the parameters of the callback method, such as

-(Nsinteger) TableView: (uitableview*) TableView numberofrowsinsection: (nsinteger) Section

    • The parameters of the callback method are only the case of the class itself, and the method name conforms to the actual meaning, such as:

-(Nsinteger) Numberofsectionsintableview: (uitableview*) TableView

    • Start with the name of the class (the case where the callback method has more than two arguments) to indicate which class this method belongs to, such as:

-(uitableviewcell*) TableView: (uitableview*) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath

    • Use did and will to notify delegate of changes that have occurred or are about to occur, such as:

-(nsindexpath*) TableView: (uitableview*) TableView Willselectrowatindexpath: (nsindexpath*) Indexpath;
-(void) TableView: (uitableview*) TableView Didselectrowatindexpath: (nsindexpath*) Indexpath;

2. Private method and variable declaration

1> Declaration Location

At the top of the. m file, define the empty category to declare
Example:

1        #import "CodeStandardViewController.h" 2//define variables and methods in this category (category) 3 @interface Codestandardviewcontroller () 4 {5  6//Declaration private Variable 7} 8  9//Private Method-(void) sampleprivatemethod;11 @end12 @im     Plementation CodeStandardViewController14//Private method implementation-(void) SamplePrivateMethod16 {//some code18 }
3. About annotations

The best code is not needing comments as far as possible by proper naming

Good code to express meaning clearly add notes where necessary

Comments need to be updated with code synchronization

If you do not name the name as much as possible, you can add some comments or mark as appropriate.

1> attribute Comment

Example:
Students
@property (nonatomic, strong) Student *student;

2> Method Declaration notes:
1     /**  2  * @brief Login Verification 3  * 4  * @param personId User name 5  * @param password Password 6  * @param Complete execution of block 7  * 8  * @return 9  */10 + (void) Loginwithpersonid: (NSString *) personId Password: (N sstring *) password complete: (void (^) (Checklogon *result)) complete;
4. About the UI layout

Using Interface Builder for interface layout

The name of the Xib file remains the same as its corresponding. h file

Xib the organization of the control in the file is reasonable, the control in the Xib file needs to have a reasonable and readable name, easy for others to understand

5. Format code 1> pointer "*" position

When defining an object, the pointer "*" is close to the variable

Example: NSString *username;

Declaration and definition of the 2> method

Leave a space between-, +, and return values, with no spaces between the method name and the first argument

-(ID) Initwithnibname: (NSString *) Nibnameornilbundle: (NSBundle *) nibbundleornil{...}
3> Code Indentation
    • Use Xcode default indentation, which is Tab = 4 spaces
    • Regularly organize code formats using the Re-indent feature in Xcode
    • Declaration of the same type variable requires a lone statement

Example:

CGFLOATORINGX = frame.origin.x; Cgfloatoringy = FRAME.ORIGIN.Y; Cgfloatlinewidth = Frame.size.width;
    • An empty line between method and method

Example:

1 #pragma mark-private methods2 3-(void) samplePrivateMethod4 {...} 5 6-(void) SampleForIf7 {...}
4> to Group method

To group methods of a class using #pragma mark-way

Example:

1      #pragma mark-private methods 2  3-(void) Sampleprivatemethod 4 {...} 5  6-(void) Sampleforif 7 {: .} 8  9-(void) SampleForWhile10 {...} One-(void) SampleForSwitch13 {...} -(void) wrongExamples16 {...} #pragma mark-public methods19-(void) Samplepublicmethodwithparam: (nsstring*) sampleParam20 {...} #pragma mark-life cycle methods23-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleo RNil24 {...} -(void) viewDidLoad27 {...} Shouldautorotatetointerfaceorientation-(BOOL): (uiinterfaceorientation) interfaceOrientation30 {...}
5> Curly Brace notation
    • For class method: Left parenthesis Write another line (follow Apple's official document)

Example:

1      -(ID) Initwithnibname: (NSString *) Nibnameornilbundle: (NSBundle *) Nibbundleornil 2 {3 Self    = [Super Initwit Hnibname:nibnameornil 4  5    Bundle:nibbundleornil]; 6  7    if (self) {8             //Custom initialization 9
   }10    return self;12}
    • For other usage scenarios: The left parenthesis follows the first line

Example:

1-(void) Sampleforif 2 {3     BOOL somecondition = YES, 4     if (somecondition) {5         //do something here 6     } 7} 8-(void) Sampleforwhile 9 {ten     int i = 0;11 while     (I <) {a         //do something here13         i = i + 1;14
   
    }15}16-(void) SAMPLEFORSWITCH17 {     SampleEnum testenum = sampleenumtwo;19     switch (testenum) {         Casesampleenumundefined:{21             //do something22             break;23         }24         casesampleenumone:{25             //Do Something26             break;27         }28         casesampleenumtwo:{29             //do something30             break;31         }32         default:{33             NSLog (@ "Warning:there is a enum type not handled properly!");             break;35         }36     }
   
    • Any part that needs to be written in curly braces must not be omitted.

Error Example:

1-(void) WrongExamples2 {3     boolsomecondition = yes;4     if (somecondition) 5         NSLog (@ "This is wrong!!!"); 6     while (somecondition) 7         NSLog (@ "This is wrong!!!"); 8}

iOS Development Code Specification

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.