命名代表抽象。

程序设计最重要的就是如何把复杂的现实世界抽象成为程序里的概念,并用算法去解决。本文对UIKit框架公开的接口进行研究探索,归纳出一套可具体化的规律,较为主观,仅供参考。

目录

  • 变量名
    • 普通
    • 数字
    • 数组
    • 布尔
    • 闭包
  • 枚举
  • 方法
    • 无返回值的方法
    • 有返回值的方法
    • 参数名
  • 类名
  • 协议
  • 代理
  • 库名

英语语法简写表

缩写 意义 例子
n 名词 font
adj 形容词 minimum
pp 分词(具有动词和形容词的特征,具有形容词功能) exhausting day / added fund. / (加粗部分)
adv 副词 automatically
v 动词 remove / add
prep 介词 of

变量名

普通
1
2
3
4
5
6
7
8
9
10
11
12
13
class UILabel {

// n
var text: String?
// n + n , text 修饰 Color
var textColor: UIColor!
// pp + n + n, highlighted / Text 修饰 Color
var highlightedTextColor: UIColor?
// n + prep + n, of lines 修饰 number
var numberOfLines: Int
// adj + n + n ,readable / content 修饰 Guide
var readableContentGuide: UILayoutGuide
}

大部分都是名词或名词短语。我想这个原因是因为:大部分变量都是用于存状态或数据的,显然它们不是一个动作或形容词程度的东西,那就只能是名词或名词短语了。

:名词短语即一个核心名词(n) + k个修饰词(n /adj /pp/ prep + n)组合而成。

数组
1
2
3
4
5
6
class UIView {
var interactions: [UIInteraction]
var layoutGuides: [UILayoutGuide]
var gestureRecognizers: [UIGestureRecognizer]?
var motionEffects: [UIMotionEffect]
}

名词或名词短语,基本规则和上一节的普通变量差不多,主要是需要注意复数形式,参照下表。

名词复数规则表格

数字

与普通变量命名规则一样,可以关注几个常用的单词。

1
2
3
4
5
var numberOfLines: Int
// n计数,Apple中这个词用得比较多
var count: Int
// 最大容量
var capacity: Int
布尔
  • is构造
  • 动宾构造
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct Double {
// is + adj
var isNormal: Bool
// is + adj
var isSubnormal: Bool
// is + n
var isZero: Bool
}

class UIView {
// is + pp
var isHidden: Bool
// is + 名词短语(adj + n)
var isExclusiveTouch: Bool
// is + 名词短语 + pp (疑问句)
var isUserInteractionEnabled: Bool

}

// Apple Pencil 的一个类
class PKCanvasView {
// is + n + adj (疑问句)
var isRulerActive: Bool
}

以上面的代码为例,Apple框架中有很多Bool变量都是使用is构造,总结下构造方法:

  • 1、is + 表语(adj/n/pp/名词短语)
  • 2、is + 名词短语 + 表语(adj/n/pp/名词短语)构成疑问句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class UIView {

// v + 名词短语(adj + n)
var hasAmbiguousLayout: Bool
// v + 名词短语(n + prep + pp)
var clearsContextBeforeDrawing: Bool
// v + n
var autoresizesSubviews: Bool
// v + 名词短语(n + pp + n)
class var requiresConstraintBasedLayout: Bool
}

class UIScrollView {
// adv + v + 名词短语(n + n + n)
var automaticallyAdjustsScrollIndicatorInsets: Bool

}

以上面的代码为例,是另一种比较常见的构造方式,即构造一个动宾短语

闭包
1
2
3
4
5
6
7
8
9
10
11
12
13
// @ios13.0
class AudioPlaybackController {
// n + Handler
var completionHandler: (() -> Void)?

}

class URLSession {
func downloadTask(
with url: URL,
completionHandler: @escaping @Sendable (URL?, URLResponse?, Error?) -> Void
) -> URLSessionDownloadTask
}
  • 名词/名词短语 + Handler
  • 或 名词/名词短语 (直接一个 completion)

枚举

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
enum Distribution {
// v
case fill
// v + adv
case fillEqually
// v + adv
case fillProportionally
}

enum TintAdjustmentMode {

// adj
case automatic
// adj
case normal
// adj
case dimmed

}

// 数字的类型
enum Style : UInt, @unchecked Sendable {

case none = 0
// adj
case decimal = 1
// n
case currency = 2
// adj
case scientific = 4
// v + prep (词组)
case spellOut = 5
}

enum PadPosition {
// (prep + n)
case beforePrefix
// prep + n
case afterPrefix
// prep + n
case beforeSuffix
// prep + n
case afterSuffix
}

@objc public enum STPSetupIntentStatus: Int {
case unknown
// v + n
case requiresPaymentMethod
// v + n
case requiresConfirmation

case requiresAction
// pp
case processing
// pp
case succeeded
// pp
case canceled
}

枚举的写法比普通变量更自由,总结归纳一下有两种:

  • 表语(名词/名词短语/adj/pp)
  • 动词可以构成的短语(v + adv/ v + n/ v + prep…)

注:普通变量名是不能直接一个adj的

方法

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
// 无返回值
class UIView {

// v + n
func addSubview(UIView)

// v + n + 状语
func bringSubviewToFront(UIView)

// v + n + 状语
func sendSubviewToBack(UIView)

// v + 状语
func removeFromSuperview()

}

struct Array {
// v + n , where 子句限定n
func removeAll(where: (Self.Element) throws -> Bool) rethrows
}

// 有返回值
struct Array {
// n
func prefix(Int) -> Self.SubSequence
// n , 参数from 限定suffix的来源
func suffix(from: Self.Index) -> Self.SubSequence
// pp
func enumerated() -> EnumeratedSequence<Self>
// pp
func joined(separator: String) -> String

}

struct Dictionary {
// n, 参数的where有点像一个从句引导词,修饰限定first
func first(where: (Self.Element) throws -> Bool) rethrows -> Self.Element?
// pp,by 引导一个状语,意思是用闭包的方法排序
func sorted(by: (Self.Element, Self.Element) throws -> Bool) rethrows -> [Self.Element]
// pp
func shuffled() -> [Self.Element]

}

class UIPickerView {
// 名词短语
func rowSize(forComponent: Int) -> CGSize
// n, 参数forRow , forComponent 限定 view
func view(forRow: Int, forComponent: Int) -> UIView?
}

可以发现方法的命名有如下规律:

  • 无返回值
    • 直接动宾结构,v + 名词短语 + 状语(用于修饰限定动词)
  • 有返回值
    • 直接和普通变量一样命名,一个名词/名词短语。
    • pp, 如shuffled sorted, 一般为过去分词,也就是给动词加ed
  • 参数
    • 普通的名词/名词短语,如:
      • joined(separator:
    • 从上面的代码可以观察到,Apple喜欢尽量用prep(介词)或Where这种从句引导词,去构造状语,限定动词的范围。如:
      • func sorted(by:
    • 从上面的代码可以观察到,Apple喜欢尽量用prep(介词)或Where这种从句引导词,去构造定语,限定名词或宾语的范围。如:
      • func view(forRow: Int, forComponent: Int)
      • func removeAll(where:

总儿言之,尽量把方法名构造得更像英语中的一句话(无返回值),或一个名词短语(有返回值)。

类名

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
protocol AdditiveArithmetic
protocol BinaryInteger
protocol BindableData
protocol CVarArg
protocol CodingKeyRepresentable
protocol Comparable
protocol CustomReflectable
protocol CustomStringConvertible
protocol Decodable
protocol Encodable
protocol EntityIdentifierConvertible
protocol Equatable
protocol ExpressibleByIntegerLiteral
protocol FixedWidthInteger
protocol Hashable
protocol LosslessStringConvertible
protocol MLDataValueConvertible
protocol MLIdentifier
// 名词短语
protocol MirrorPath
// adj

// adj
protocol Sendable
protocol SignedInteger
protocol SignedNumeric
// adj
protocol Strideable

上面是Swift中Int遵守的协议,可以观察到上面的协议命名有如下规律:

  • adj(形容词)/名词短语
  • 喜欢一种表达:v + 词缀able
    • Sendable
    • Hashable
    • Comparable
    • Decodable

1、很好理解,因为一般实现这个协议的时候,我们会写:

1
2
3
4
struct Int: Sendable, Hashable, SIMDScalar, Encodable {

}

想一想,Int是一个名词,Sendable, Hashable, SIMDScalar, Encodable是用于修饰限定Int的,那一般会用什么?最适合的答案,我想一定是adj和n

2、v + 词缀able 构成的单词往往代表能…的,表达拥有一种能力,其实这里很多单词应该都是Apple自己造的。如,Hashable代表Int是 能Hash的,Encodable代表Int是能Encode的。

代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
protocol UITableViewDataSource {

func tableView(UITableView, cellForRowAt: IndexPath) -> UITableViewCell

func tableView(UITableView, titleForHeaderInSection: Int) -> String?

func tableView(UITableView, titleForFooterInSection: Int) -> String?

func tableView(UITableView, moveRowAt: IndexPath, to: IndexPath)

func tableView(UITableView, commit: UITableViewCell.EditingStyle, forRowAt: IndexPath)
}

protocol {
func tableView(UITableView, willDisplayHeaderView: UIView, forSection: Int)

func tableView(UITableView, willDisplayFooterView: UIView, forSection: Int)

}

其实我感觉Swift里很少有Delegate了?,不过为了完整性,也稍微研究了一下。主要规律:

  • 第一个参数都是方法发起者的实例对象,上例中就是tableView

库名

  • UIKit
  • ARKit
  • GameKit

很多后缀会加上Kit,然后比较短。其中,Kit的翻译:配套元件; 成套工具; 成套设备; 全套衣服及装备;