1.0自定義輸出日志(log)
和OC不同的是绰垂,swift沒(méi)有所謂的pch文件,即時(shí)新建了也產(chǎn)生不了什么作用火焰,而且swift3.0也沒(méi)有預(yù)編譯命令#define劲装,只能通過(guò)最新的語(yǔ)法來(lái)實(shí)現(xiàn)自定義log。
1.1 配置項(xiàng)目,在Active Compilation Condition中將Debug選項(xiàng)改為DEBUG (默認(rèn)就是這個(gè),如果不是就修改)
1.2 新建一個(gè)Swift的頭文件昌简,可以在該文件中進(jìn)行一些全局變量和全局函數(shù)的聲明和實(shí)現(xiàn)占业。
我將Swift頭文件命名為"AllMacro",在這里我做與自定義log有關(guān)的函數(shù)定義與實(shí)現(xiàn)。
func LogMsgWithDebug(filePath:String=#file, rowCount:Int=#line) {
? ? ? ?#if DEBUG
? ? ? ? ? ? let fileName = (filePath as NSString).lastPathComponent.replacingOccurrences(of:".swift", with:"")
? ? ? ? ? ? print(fileName +"類(lèi)中第"+"\(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 +"類(lèi)中第"+"\(rowCount)"+"行,打印信息是:"+"\(message)")
? ? #endif
}
測(cè)試調(diào)用如下: