Swift中自定义Log

2016-01-04  本文已影响0人  飞飞hang

OC系统会自定添加宏,而swift没有,如果我们想暴力调试可以进行以下操作,一般我们把这个函数写在AppDelegate.swift文件中,供其他文件调用

func HFLog<T>(message: T, fileName: String = __FILE__, methodName: String =  __FUNCTION__, lineNumber: Int = __LINE__)
{
    #if DEBUG
    let str : String = (fileName as NSString).pathComponents.last!.stringByReplacingOccurrencesOfString("swift", withString: "")
    print("\(str)\(methodName)[\(lineNumber)]:\(message)")
    #endif
}

还要配置以下宏


Snip20160104_18.png

假如我在一个函数中打印这个"我是靓仔"

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
       
        HFLog("我是靓仔")
        return true
    }

最终会输出

AppDelegate.application(_:didFinishLaunchingWithOptions:)[19]:我是靓仔

注:依次是类名.方法名.行号.内容.

上一篇下一篇

猜你喜欢

热点阅读