您的位置:首页 > 移动开发 > Swift

Swift中自定义Log

2016-05-31 11:21 369 查看
Swift中自定义Log

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
}

还要配置以下宏

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

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

HFLog("我是靓仔")
return true
}

最终会输出

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

注:依次是类名.方法名.行号.内容.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: