iOS code specification (OC and Swift)

Source: Internet
Author: User
Tags diff

The following is the iOS code specification problem, if you feel good, you can directly use the project, there are different views can be discussed below.

Believe that many people work the most annoying is the code is not standardized, naming is not standardized, have seen a VC has 3 buttons are named Button1, Button2, Button3, the full text No comments, to see the code logic to know the meaning of the three buttons, I am also drunk!

The following specifications are determined by the comparison of death, we can according to the style of their own team to modify. This article is primarily a code specification for OC, and several are the specifications of Swift.

The code specifications for OC and Swift are as follows:

first, the VC life cycle module sequencing

1. Attention

#pragma mark-(plus line) is used to differentiate between large modules and capitalize the first letter.

#pragma mark (no dash) is a small module, mark and the first line of the function do not have a blank line, no module does not write mark.

2. Basic standards

#pragma mark-life Cycle

Before the Viewdidload

Viewdidload

After the Viewdidload

Dealloc

#pragma mark-(delegate provided by the system)

#pragma mark-(custom delegate)

#pragma mark-(third-party delegate)

#pragma mark-event response//Here mainly put ibaction, notice, gesture, etc.

#pragma mark-public method

#pragma mark-private method

#pragma mark-getters and Setters

3. Commonly used in projects

#pragma mark-life Cycle

#pragma mark-override

#pragma mark-uitableviewdelegate

#pragma mark-customdelegate

#pragma mark-event Response

#pragma mark-public Methods

#pragma mark-getters and Setters

second, VC to join the Dealloc method

If you exit the page and do not execute the Dealloc method, there is a memory leak problem, so add the Dealloc method and print the log. Develop habits.

third, control unified suffix name

1, the commonly used suffix name rule

In addition to the following enrichment, the other only removes the UI (as Uitextfield writes: TextField):

Uibutton:btn

Uilabel:lbl

Uitableviewcell:cell

Image:img

Uiviewcontroller:vc

Uitableviewcontroller:tablevc

2. Other

2.1, create a new Uiviewcontroller file suffix to Viewcontroller

2.2, create a new Uitableviewcontroller file suffix to Viewcontroller

2.3, multiple Tags: With enumeration definition, suffix: tag

Iv. Uniform Method name

This specification is for everyone to see each page, a look at the method is to do what, each team should be based on their own characteristics to customize.

If there are several people in the team who are not well-named (written method names do not understand), you need to follow this one to the unified naming. This can be omitted if the members of the team are able to name the article well.

1. Initialize all controls unify one method name: Initallview

2, initialize the data method name: InitData

3, request network, unified method name prefix

VC only one network request:: Loadnetdata

Multiple plus suffixes: load****netdata

4, the Network layer class plus suffix: ***request

5, model should be added suffix model.

6. Click the event prefix: Tap

v. Documents #import and @end

1, import structured order, after @end left 1 return position, @end before the last function empty 1 lines

2. Import the class itself first. h again <>, after "", include is written together

#import "AppDelegate.h"

#import <arpa/inet.h>

#import "OpenUDID.h"

six, line break and parentheses

1. Multiple protocol names are too long to rely on Xcode to automatically scale.

2, the method does not have parameters or parameters in a row, after the parentheses do not wrap, such as:-(ID) responseobject {.

The method has multiple arguments, the argument wraps, and the parentheses wrap, such as:

-(Afhttprequestoperation *) PUT: (NSString *) urlstring parameters: (ID) parameters  Success: ( void ID responseobject)) Success    failure: (void (^) (afhttprequestoperation *operation, Nserror *error)) failure{
}

vii. specification of @property

1, weak, copy of the use of more attention, Nsarray, nsdictionary, nsstring attribute definition as far as possible with copy, which is to prevent the assignment to it is variable data, if the variable data changes, then the property will also change.

2, the variable first by functional classification and then by the type of placement, you can consider the right alignment

Eight, define module

1, each define by the module classification, according to the natural order of letters

ix. norms of typedef

You can choose to use the following format when deciding more:

typedef ns_options (Nsuinteger, sdwebimageoptions) {    sdwebimageretryfailed = 1 << 0,    sdwebimagelowpriority = 1 << 1,    sdwebimagecachememoryonly = 1 << 2,    sdwebimageprogressivedownload = 1 << 3,    sdwebimagerefreshcached = 1 << 4,    sdwebimageavoidautosetimage = 1 << 11};

10. Common naming rules

1. Class, method naming

The first letter of the class name is capitalized, the method is lowercase, the parameter in the method is lowercase, the second parameter name starts with a "with",-the + sign is preceded by a space

At the same time try to make the name of the method read like a sentence, can convey the meaning of the method

Do not prefix "get" before taking a value method

Gestures with "tapgesture", "tap" similar to the notification prefix "notification", "note" similar, not with _

2. Variable naming

Meaning completeness, name of the variable btn, IMG type at the end of the feature word, such as softbtn, HOMEVC, redimg

3. Picture catalogue and naming

Btn button, by default, highlight as normal status picture name finally with the suffix "_h"

First level directory (function) _ Level two directory (BTN or IMG) _ Level Three directory (features), such as Goodlist_btn_greenarrow

11. Debugging Usage

1. TODO

The temporary processing scheme or the uncommitted implementation plus TODO.

2, Nsassert

Use assertion Nsassert to track exceptions: Nsassert (thedate! = nil, @ "Argument must be Non-nil");

3, Instruments

Check the memory, elapsed time, CPU, GPU and other usage of your module.

12, to pay attention to the

1. Initialize "id" to write "Instancetype"

2, int with nsinteger,float with cgfloat

3, ":" Do not add a space, the function of the first curly braces do not wrap

4, Static const NSString * klnasss = @ "";

5, as long as "," You must write a space after

6, annotation flexible processing, complex methods need to explain clearly

7, irrelevant methods, variables are put in the. m

8, try not to directly use the number, string assignment, unless the back with a description of the value of the origin

9, fixed string, number. If only one place is used, write dead. Try to annotate.

If a page is used multiple times, it is defined on this page define

Constants are defined if multiple pages are used (multiple places need to be modified at the same time)

10. If else's problem.

Write the whole note here

IfElse// There's no writing here. 

13, use block to pay attention to circular reference

Although most of the local variables inside the self without weak will not cause a circular reference (TableView display cell when the exception cellforrowatindexpath), but still recommend that everyone add __weak.

14, the use of Reviewboard process

First, the diff, then the diff submit Reviewboard, and then Submit SVN (when tagged with cr: version)

15. Important common functions. Note format for h:
/* *** */

In this way, you can use shortcut keys to view the contents of the comments, generally do not write this.

16, Scrollviewdidscroll

To override the page of the Scrollviewdidscroll method, in dealloc, scrollview.delegate = nil. Otherwise clicking the Back button will crash. IOS 9 Fixes an issue with scrollviewdidscroll crashes.

17, the swift file printing log with print, must not use NSLog

Because Swift uses NSLog to record the system log, it can be obtained by hackers.

Using a macro definition to mask the NSLog log output in the release environment is useless for swfit.

iOS code specification (OC and Swift)

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.