1、Tips
-
自定义 print:CustomStringConvertible 协议;debugPrint:CustomDebugStringConvertible 协议。
-
po会优先调用 debugPrint
-
打印信息
| OC | Swift |
|---|---|
_cmd |
#function |
__FILE__ |
#file |
__LINE__ |
#line |
__COLUMN__ |
#column |
let file = (#file as NSString).lastPathComponent
print(file + " " + #function + " [Line: \(#line)]")
// Prints "ViewController.swift viewDidLoad() [Line: 57]"
//长地址 0x0000000281e51f00
print(object_getClassName(self))
//类名 ViewController
print(type(of: self))
//<应用名.类名: 地址> <Demo.ViewController: 0x102d0b230>
print(self)
print(self.debugDescription)
- 自定义print打印函数
func MLog<T>(_ message: T, file: String = #file, function: String = #function, line: Int = #line){
#if DEBUG
let file = (file as NSString).lastPathComponent.split(separator: ".").first!;
print("\(file) \(function) [Line: \(line)]: \(message)");
#endif
}
2、打印方法
2.1> print
print 不打印多余日志,可使用字符串插值 \(),
NSLog:[日期戳] [时间戳] [工程名] [进程 ID: 线程 ID] [消息]
let name = "aa"
print("print: \(name)")
NSLog("nslog: %@", name)
//Output:
print : aa
2018-03-27 13:24:23.198534+0800 demo[25446:1300804] nslog :aa
2.1.1> 原型方法
这个重载 print 采用可变长度的参数列表,后跟separator 和 terminator参数 - 两者都有默认值。
- separator 分隔符,是用于将每个元素的表示连接items 成单个字符串的字符串。默认情况下,这是一个空格(" ")。
- terminator 终结符,是附加到打印表示的末尾的字符串。默认情况下,这是换行符("\n")。
- output 采用 Target 符合协议的泛型类型的可变实例。符合的类型的实例 可以传递给函数以从标准输出中捕获和重定向字符串。
func print<Target>(_ items: Any...,
separator: String = default,
terminator: String = default,
to output: inout Target)
where Target : TextOutputStream
//e.g:
print(1.0, 2.0, 3.0, 4.0, 5.0, separator: " ... ")
// 1.0 ... 2.0 ... 3.0 ... 4.0 ... 5.0
for n in 1...5 {
print(n, terminator: "")
}
// 12345
2.1.2> TextOutputStream
print 不止支持输出打印还可以写入到变量中。
使用场景:将打印和调试打印件写入文件
实现自定义 TextOutputStream 类型
var str = "str: "
print(1...5, to: &str)
print(str)
// str: 1...5\n
var separated = "s: "
print(1.0, 2.0, 3.0, 4.0, 5.0, separator: "...", to: &separated)
print(separated)
// s: 1.0...2.0...3.0...4.0...5.0\n
var numbers = "num: "
for n in 1...5 {
print(n, terminator: "", to: &numbers)
}
print(numbers)
// num: 12345\n
2.2> debugPrint
官方的解释是将最适合调试的给定项的文本表示写入标准输出。
与print的不同是:debugPrint添加了对调试类型信息等有用的附加信息。
print(1...5)
// 1...5
debugPrint(1...5)
// ClosedRange(1...5)
2.3> dump
使用映射将给定对象的内容转储到标准输出。
能够经对象里的东西展开打印,也可以控制参数,来控制展示层数,尤其是在获取大批量数据的时候,全部打印太多,打印部分正好。
/// 使用给定对象的镜像将给定对象的内容转储到标准输出
///
/// - Parameters:
/// - value: 要输出到target流的值
/// - name: 写入内容时使用的标签value。默认是nil
/// - indent: 用作输出的每一行的缩进的空格数。默认是0
/// output. The default is `0`.
/// - maxDepth: 写入具有嵌套组件的值的内容时要下降的最大深度。默认是 Int.max
/// - maxItems: 要写入完整内容的最大元素数。默认是 Int.max
/// - Returns: The instance passed as `value`.
public func dump<T>(_ value: T, name: String? = nil, indent: Int = 0, maxDepth: Int = .max, maxItems: Int = .max) -> T
dump(UIView())
// output
- <UIView: 0x103510bb0; frame = (0 0; 0 0); layer = <CALayer: 0x28021c320>> #0
- super: UIResponder
- super: NSObject
3、自定义 description
想让 print 输出的东西能表现更多信息,就需要自定义输出了。
print -- CustomStringConvertible 协议
debugPrint -- CustomDebugStringConvertible 协议
po会优先调用 debugPrint
class Person: CustomStringConvertible {
var age: Int = 10
var description: String {
return "\(type(of: self)) age = \(age)"
}
}
print(Person())
// Prints "Person age = 10"













网友评论