-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjective-c coding style
152 lines (115 loc) · 3.73 KB
/
Objective-c coding style
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Objective-c coding style
#### 1. 统一使用US英语
<p style='color:#0c0'>符合规则的:</p>
UIColor *myColor = [UIColor whiteColor];
<p style='color:#c00'>不符合规则的:</p>
UIColor *myColour = [UIColor whiteColor];
UIColor *yanse = [UIColor whiteColor];
#### 2. 使用#pragma mark - 对方法进行分类
<p style='color:#0c0'>符合规则的:</p>
#pragma mark - Lifecycle
- (instancetype)init {}
- (void)dealloc {}
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
- (void)didReceiveMemoryWarning {}
#pragma mark - Custom Accessors
- (void)setCustomProperty:(id)value {}
- (id)customProperty {}
#pragma mark - IBActions
- (IBAction)submitData:(id)sender {}
#pragma mark - Public
- (void)publicMethod {}
#pragma mark - Private
- (void)privateMethod {}
#pragma mark - Protocol conformance
#pragma mark - UITextFieldDelegate
#pragma mark - UITableViewDataSource
#pragma mark - UITableViewDelegate
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone {}
#pragma mark - NSObject
- (NSString *)description {}
#### 3. 在.h文件中方法以及#pragma mark -间不空行,@property间不空行,但两部分之间空一行
<p style='color:#0c0'>符合规则的:</p>
@property NSString (nonatomic) *name;
@property NSInteger length;
#pragma mark - Lifecycle
- (instancetype)init {}
- (void)dealloc {}
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
- (void)didReceiveMemoryWarning {}
<p style='color:#c00'>不符合规则的:</p>
@property NSString (nonatomic) *name;
@property NSInteger length;
#pragma mark - Lifecycle
- (instancetype)init {}
- (void)dealloc {}
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
- (void)didReceiveMemoryWarning {}
#### 4. 在.m文件中方法以及#pragma mark -间空一行
<p style='color:#0c0'>符合规则的:</p>
#pragma mark - Lifecycle
- (instancetype)init {}
- (void)dealloc {}
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
- (void)didReceiveMemoryWarning {}
<p style='color:#c00'>不符合规则的:</p>
#pragma mark - Lifecycle
- (instancetype)init {}
- (void)dealloc {}
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
- (void)didReceiveMemoryWarning {}
#### 5. @property的默认属性值不写,只注明非默认值
<p style='color:#0c0'>符合规则的:</p>
@property (weak, nonatomic) NSString *property1;
@property (nonatomic) NSString *property2;
@property NSString *property3;
<p style='color:#c00'>不符合规则的:</p>
@property (strong, atomic) NSString *property1;
#### 6. 选择,循环结构语句块与上一条语句之间空一行
<p style='color:#0c0'>符合规则的:</p>
NSInteger a;
NSInteger b;
if (a > b) {
NSLog(@"a > b");
}
<p style='color:#c00'>不符合规则的:</p>
NSInteger a;
NSInteger b;
if (a > b) {
NSLog(@"a > b");
}
#### 7. 缩进使用4个空格,可在Xcode->Preferences->Text Editing中设置
#### 8. 方法及其他语句块的大括号总是在同一行语句打开但在新行中关闭
<p style='color:#0c0'>符合规则的:</p>
if (success) {
//Do something
} else {
//Do something else
}
<p style='color:#c00'>不符合规则的:</p>
if (success)
{
//Do something
} else
{
//Do something else
}
#### 9. 选择,循环结构语句块中关键字,条件,大括号之间都有一个空格
<p style='color:#0c0'>符合规则的:</p>
if (success) {
//Do something
} else {
//Do something else
}
<p style='color:#c00'>不符合规则的:</p>
if(success){
//Do something
}else{
//Do something else
}