iOS Swift和OC專案中自定義Log

zhf_Zachariah發表於2017-12-13

通常在實際專案中,為了檢測程式碼邏輯的實現是否正確,我們可能會在需要的地方進行資料列印來驗證。在除錯階段我們可以這麼做,但是專案釋出之後還要一個一個註釋掉麼?想想都覺得累,那麼問題來了,有沒有一種方法能夠讓列印在Debug階段正常列印,在Release階段就自動“消失”呢?

  • OC中的自定義Log

廢話不多說,看程式碼 = = 當然名字你可以隨便起,只要你認識 = =

/***除錯狀態(BUG)****/
#ifdef DEBUG
#define ZZLog(...) NSLog(__VA_ARGS__)   //除錯狀態NSLog 就變為ZZLog輸出
#else
#define ZZLog(...)                      //非除錯狀態輸出ZZLog為空
#endif
複製程式碼

OC中的自定義Log就是這麼簡單,至少我是這麼做的 = =

  • Swift中的自定義Log

看過OC中的自定義Log,你可能覺得swift中也是這麼簡單,那就錯了,不得不說swift的路子還是野。。。首先呢OC中的自定義Log我們寫成了巨集定義,會放在.h或者.pch檔案中。那麼問題來了,swift中沒有巨集定義這麼一說啊,該怎麼辦呢?

為了解決這個問題,我們把自定義Log寫成一個全域性函式,像這樣--

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
}


//這個位置呢就是寫全域性函式的地方,因為程式生命週期的原因一般寫在AppDelegate裡邊
//全域性函式 - 只要不寫成某個類裡邊的方法 - 最好寫在APPDelegate裡邊
func test(){
    //程式碼段
}

//寫的不對,請提出意見,要不你來打我呀? = =
複製程式碼

解決了這個問題我們再來看看自定義Log

swift中的列印格式就是這麼任性,除了結果什麼都木有。。。

swift中的列印格式.png

為了列印看到的資訊更加的清楚,我們這兒樣做

//獲取列印所在的檔案
 let file = (#file as NSString).lastPathComponent
 print("\(file): -test")
複製程式碼

獲取列印所在的檔案.png

//獲取列印所在的方法
 print("\(#function): -test")
複製程式碼

獲取列印所在的方法.png

//獲取列印所在的行數
print("\(#line): -test")
複製程式碼

獲取列印所在的行數.png

為了方便起見,把他們仨拼接起來.這樣就可以清晰的看到我們所需要列印資料的地方

print("\((#file as NSString).lastPathComponent):[\(#function)](\(#line)) -zhf")
複製程式碼

螢幕快照 2016-08-24 上午9.05.20.png

接下來重要的一步是設定Other Swift Flags

swift自定義Log的設定.png

結合全域性函式的書寫,我們就可以完成自定義Log了

// MARK: - 註釋>自定義Log
func ZHFLog<T>(message : T,file : String = #file,funcName : String = #function,lineNum : Int = #line) {
    
    #if DEBUG  //設定完成後就可以了
        
    let fileName = (file as NSString).lastPathComponent
        
    print("\(fileName):[\(funcName)](\(lineNum)) -\(message)")
        
    #endif
}
複製程式碼

在上面的方法中我們新增了一個message的引數,主要是為了適應多格式的引數輸入

測試下 - -,在Debug環境下,列印出了我們想要的結果;在release環境下,則不列印。

測試結果.png

再補充下swift的註釋該怎麼寫

1.// MARK: 
2.///
這種格式的註釋,在寫程式碼時會有註釋提示
3.使用VVDocuments註釋
複製程式碼

相關文章