swift封装打印函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| let dateFormatter = DateFormatter()
func DLog<T>(_ message:T, file:String = #file, function:String = #function, line:Int = #line) { #if DEBUG let fileName = (file as NSString).lastPathComponent dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS" let datestr = dateFormatter.string(from: Date())
let consoleStr = "\(datestr) [\(fileName):\(line)][\(function)]\(message)" print(consoleStr) #endif }
|
打印对象地址
1 2 3 4 5 6 7
| extension String { static func pointer(_ object: AnyObject?) -> String { guard let object = object else { return "nil" } let opaque: UnsafeMutableRawPointer = Unmanaged.passUnretained(object).toOpaque() return String(describing: opaque) } }
|
使用
1 2
| DLog(self) DLog("self p:\(Unmanaged.passUnretained(self).toOpaque())")
|
打印如下:
1 2
| 2019-02-27 11:58:57.378 [ViewController.swift:30][viewDidAppear]<demo.ViewController: 0x1059c2170> 2019-02-27 11:58:57.383 [ViewController.swift:31][viewDidAppear]self p:0x00000001059c2170
|
不过还是推荐Swift - 日志框架XCGLogger的使用详解