Swift自定义(可变参)打印函数
2018-08-22 17:48 法子 阅读(553) 评论(0) 编辑 收藏 举报/// 单参数打印函数 /// /// - Parameters: /// - message:打印 /// - file: 文件名,默认值:#file /// - line: 第几行,默认值:#line /// - method: 函数名,默认值:#function func printLog(_ message: Any, file: String = #file, line: Int = #line, method: String = #function) { #if DEBUG print("\((file as NSString).lastPathComponent)[\(line)], \(method): \(message)") #endif } //调用 let a = 2 printLog(a) //打印结果:ViewController.swift[59], printLog(): 2 /// 可变参打印函数 /// /// - Parameters: /// - items: Zero or more items to print. /// - separator: A string to print between each item. The default is a single space (`" "`). /// - terminator: The string to print after all items have been printed. The default is a newline (`"\n"`). /// - file: 文件名,默认值:#file /// - line: 第几行,默认值:#line /// - method: 函数名,默认值:#function func printLog(_ items: Any..., separator: String = " ", terminator: String = "\n", file: String = #file, line: Int = #line, method: String = #function) { #if DEBUG //如果不怕打印结果有大括号[4, "abc", [1, 2, 3]],可以直接一句话 //print("\((file as NSString).lastPathComponent)[\(line)], \(method):", items) print("\((file as NSString).lastPathComponent)[\(line)], \(method):", terminator: separator) var i = 0 let j = items.count for a in items { i += 1 print(a, terminator:i == j ? terminator: separator) } #endif } //调用 printLog(4, "abc", [1,2,3]) //打印结果:ViewController.swift[89], printLog(): 4 abc [1, 2, 3]