通常在實(shí)際項(xiàng)目中观游,為了檢測(cè)代碼邏輯的實(shí)現(xiàn)是否正確稽屏,我們可能會(huì)在需要的地方進(jìn)行數(shù)據(jù)打印來驗(yàn)證奖慌。在調(diào)試階段我們可以這么做抛虫,但是項(xiàng)目發(fā)布之后還要一個(gè)一個(gè)注釋掉么?想想都覺得累简僧,那么問題來了建椰,有沒有一種方法能夠讓打印在Debug階段正常打印,在Release階段就自動(dòng)“消失”呢岛马?
- OC中的自定義Log
廢話不多說棉姐,看代碼 = =
當(dāng)然名字你可以隨便起,只要你認(rèn)識(shí) = =
/***調(diào)試狀態(tài)(BUG)****/
#ifdef DEBUG
#define ZZLog(...) NSLog(__VA_ARGS__) //調(diào)試狀態(tài)NSLog 就變?yōu)閆ZLog輸出
#else
#define ZZLog(...) //非調(diào)試狀態(tài)輸出ZZLog為空
#endif
OC中的自定義Log就是這么簡(jiǎn)單啦逆,至少我是這么做的 = =
- Swift中的自定義Log
看過OC中的自定義Log,你可能覺得swift中也是這么簡(jiǎn)單伞矩,那就錯(cuò)了,不得不說swift的路子還是野蹦浦。扭吁。。首先呢OC中的自定義Log我們寫成了宏定義盲镶,會(huì)放在.h或者.pch文件中侥袜。那么問題來了,swift中沒有宏定義這么一說啊溉贿,該怎么辦呢枫吧?
為了解決這個(gè)問題,我們把自定義Log寫成一個(gè)全局函數(shù)宇色,像這樣--
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:.
}
}
//這個(gè)位置呢就是寫全局函數(shù)的地方九杂,因?yàn)槌绦蛏芷诘脑蛞话銓懺贏ppDelegate里邊
//全局函數(shù) - 只要不寫成某個(gè)類里邊的方法 - 最好寫在APPDelegate里邊
func test(){
//代碼段
}
//寫的不對(duì),請(qǐng)?zhí)岢鲆庖娦洌荒銇泶蛭已剑?= =
解決了這個(gè)問題我們?cè)賮砜纯醋远xLog
swift中的打印格式就是這么任性例隆,除了結(jié)果什么都木有。抢蚀。镀层。
為了打印看到的信息更加的清楚,我們這兒樣做
//獲取打印所在的文件
let file = (#file as NSString).lastPathComponent
print("\(file): -test")
//獲取打印所在的方法
print("\(#function): -test")
//獲取打印所在的行數(shù)
print("\(#line): -test")
為了方便起見皿曲,把他們仨拼接起來.這樣就可以清晰的看到我們所需要打印數(shù)據(jù)的地方
print("\((#file as NSString).lastPathComponent):[\(#function)](\(#line)) -zhf")
接下來重要的一步是設(shè)置Other Swift Flags
結(jié)合全局函數(shù)的書寫唱逢,我們就可以完成自定義Log了
// MARK: - 注釋>自定義Log
func ZHFLog<T>(message : T,file : String = #file,funcName : String = #function,lineNum : Int = #line) {
#if DEBUG //設(shè)置完成后就可以了
let fileName = (file as NSString).lastPathComponent
print("\(fileName):[\(funcName)](\(lineNum)) -\(message)")
#endif
}
在上面的方法中我們添加了一個(gè)message的參數(shù)吴侦,主要是為了適應(yīng)多格式的參數(shù)輸入
測(cè)試下 - -,在Debug環(huán)境下,打印出了我們想要的結(jié)果坞古;在release環(huán)境下备韧,則不打印。
再補(bǔ)充下swift的注釋該怎么寫
1.// MARK:
2.///
這種格式的注釋痪枫,在寫代碼時(shí)會(huì)有注釋提示
3.使用VVDocuments注釋