Swift-CustomPrint

自定义打印协议CustomStringConvertibleCustomDebugStringConvertible

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Person {
var age: Int = 10
}

extension Person: CustomStringConvertible {
var description: String {
"person age: \(age)"
}
}

extension Person: CustomDebugStringConvertible {
var debugDescription: String {
"debugPerson age: \(age)"
}
}

调用

1
2
3
let p = Person()
print(p)
debugPrint(p)

打印结果

1
2
person age: 10
debugPerson age: 10

当处于Release模式的时候,debugPrint也仍然会输出。目前看不出区别

当在控制台po的时候,调用的是CustomDebugStringConvertibledebugDescription方法