自定义打印协议CustomStringConvertible和CustomDebugStringConvertible
class Person {
var age: Int = 10
}
extension Person: CustomStringConvertible {
var description: String {
"person age: \\(age)"
}
}
extension Person: CustomDebugStringConvertible {
var debugDescription: String {
"debugPerson age: \\(age)"
}
}
调用
let p = Person()
print(p)
debugPrint(p)
打印结果
person age: 10
debugPerson age: 10
当处于Release模式的时候,debugPrint也仍然会输出。目前看不出区别
当在控制台po的时候,调用的是
CustomDebugStringConvertible的debugDescription方法