Swift_自定義log
阿新 • • 發佈:2018-12-05
print(#file) //獲取當前當前print所在的檔案路徑
print(#function) //獲取s當前print所在的方法名稱
print(#line) //獲取當前pring所在的行號
根據這些我們可以建立建立一個方法
/// - Parameters:
/// - message: 列印的資訊
/// - fileName: 檔名稱
/// - methodName: 方法名
/// - lineNumber: 行號
func NSLog<T>(message : T, fileName : String = #file, methodName : String = #function, lineNumber : Int = #line) {
#if DEBUG
// print("\((fileName as NSString).pathComponents.last!.description)--\(methodName)-[\(lineNumber)]: \(message)")
print("\(methodName)-[\(lineNumber)] 列印資料: \(message)")
#endif
}
這個方法我們放在自己建立的 Swift File這樣就能全域性使用了。然後我們需要在專案中進行一下設定確保只有在Debug模式下才列印資料