Skip to content

Commit 3176244

Browse files
committed
翻译完'命名'部分
Signed-off-by: mrahmiao <mrahmiao@gmail.com>
1 parent 2ffa0da commit 3176244

File tree

1 file changed

+89
-2
lines changed

1 file changed

+89
-2
lines changed

zh_style_guide.md

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* [间隔](#间隔)
1313
* [注释](#注释)
1414
* [注释](#注释)
15-
* [类前缀](#类前缀)
15+
* [类前缀](#类前缀)
1616
* [类与结构体](#类与结构体)
1717
* [函数声明](#函数声明)
1818
* [闭包](#闭包)
@@ -45,6 +45,7 @@ var colour = "red"
4545
* 方法的花括号以及其它花括号(`if`/`else`/`switch`/`while`等等)总是跟语句在同一行开始,但是在新的一行中结束。
4646

4747
**优选:**
48+
4849
```swift
4950
if user.isHappy {
5051
//Do something
@@ -54,6 +55,7 @@ if user.isHappy {
5455
```
5556

5657
**不建议使用:**
58+
5759
```swift
5860
if user.isHappy
5961
{
@@ -66,6 +68,91 @@ else {
6668

6769
* 方法之间应该总是用一空白行进行分隔以提高视觉以及结构上的清晰度。方法中的空白符用来隔离功能块,但是如果一个方法中存在太多这种功能块时,通常也意味着你需要将它重构为多个方法了。
6870

69-
71+
## 注释
72+
73+
* 在需要的时候使用注释说明一块代码**为什么**这么做。注释必须时刻跟进代码,不然删了得了。
74+
75+
* 因为代码应该尽可能的自文档化,所以避免在代码中使用成块的注释。**例外:该规则不适用与用于生成文档的成块注释。**
76+
77+
## 命名
78+
79+
使用驼峰法为类、方法、变量等等取一个描述性的名字。模块范围的类名以及常量名称要以大写字母开头,而方法名跟变量名则应该以小写字母开头。
80+
81+
**优选:**
82+
83+
```swift
84+
let MaximumWidgetCount = 100
85+
86+
class WidgetContainer {
87+
var widgetButton: UIButton
88+
let widgetHeightPercentage = 0.85
89+
}
90+
```
91+
92+
**不建议使用:**
93+
94+
```swift
95+
let MAX_WIDGET_COUNT = 100
96+
97+
class app_widgetContainer {
98+
var wBut: UIButton
99+
let wHeightPct = 0.85
100+
}
101+
```
102+
103+
对于普通函数以及构造函数名称,除非上下文含义非常清楚,对所有的参数都加以命名是更为推荐的做法。如果外部参数名称可以使得函数调用更具有可读性,那么请带上它。
104+
105+
```swift
106+
func dateFromString(dateString: NSString) -> NSDate
107+
func convertPointAt(#column: Int, #row: Int) -> CGPoint
108+
func timedAction(#delay: NSTimeInterval, perform action: SKAction) -> SKAction!
109+
110+
// 会被这样调用
111+
dateFromString("2014-03-14")
112+
convertPointAt(column: 42, row: 13)
113+
timedAction(delay: 1.0, perform: someOtherAction)
114+
```
115+
116+
对于方法,遵循苹果的命名标准,在方法名中提及第一个参数:
117+
118+
```swift
119+
class Guideline {
120+
func combineWithString(incoming: String, options: Dictionary?) { ... }
121+
func upvoteBy(amount: Int) { ... }
122+
}
123+
```
124+
125+
在文章中(包括教程,书以及评论)提到函数时,请从调用者的视觉来考虑,将所有的必要参数名都包含进来:
126+
127+
```
128+
The dateFromString() function is great.
129+
Call convertPointAt(column:, row:) from your init() method.
130+
The return value of timedAction(delay:, perform:) may be nil.
131+
Guideline objects only have two methods: combineWithString(options:) and upvoteBy()
132+
You shouldn't call the data source method tableView(cellForRowAtIndexPath:) directly.
133+
```
134+
135+
### 类前缀
136+
137+
Swift中的类型会自动加入包含它们的模块的命名空间。所以即使是为了最小化命名冲突的可能性,前缀也是不必要的。如果来自于不同模块的两个名称冲突了,可以通过在名称前面加上模块名以消除歧义:
138+
139+
```swift
140+
import MyModule
141+
142+
var myClass = MyModule.MyClass()
143+
```
144+
145+
**不应该**在自己创建的类型上加前缀。
146+
147+
如果需要将Swift类型暴露在Objective-C环境中使用,请按照以下方式提供合适的前缀(前缀的命名请参考[Objective-C风格指南](https://github.com/raywenderlich/objective-c-style-guide)):
148+
149+
```swift
150+
@objc (RWTChicken) class Chicken {
151+
...
152+
}
153+
```
154+
155+
156+
70157

71158
[objc-style-guide]: https://github.com/raywenderlich/objective-c-style-guide

0 commit comments

Comments
 (0)