Swift3.0之其他開發需要

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

}

測試調用如下:

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容