1.0自定義輸出日志(log)
和OC不同的是,swift沒有所謂的pch文件,即時新建了也產生不了什么作用,而且swift3.0也沒有預編譯命令#define,只能通過最新的語法來實現自定義log。
1.1 配置項目,在Active Compilation Condition中將Debug選項改為DEBUG (默認就是這個,如果不是就修改)
1.2 新建一個Swift的頭文件,可以在該文件中進行一些全局變量和全局函數的聲明和實現。
我將Swift頭文件命名為"AllMacro",在這里我做與自定義log有關的函數定義與實現。
func LogMsgWithDebug(filePath:String=#file, rowCount:Int=#line) {
? ? ? ?#if DEBUG
? ? ? ? ? ? let fileName = (filePath as NSString).lastPathComponent.replacingOccurrences(of:".swift", with:"")
? ? ? ? ? ? print(fileName +"類中第"+"\(rowCount)"+"行: ")
? ? #endif
}
func LogMsgWithDebug<Msg>(filePath:String=#file, rowCount:Int=#line,_message:Msg) {
? ? ? ? #if DEBUG
? ? ? ? ? ? ?let fileName = (filePathasNSString).lastPathComponent.replacingOccurrences(of:".swift", with:"")
? ? ? ? ? ? print(fileName +"類中第"+"\(rowCount)"+"行,打印信息是:"+"\(message)")
? ? #endif
}
測試調用如下: