在iOS开发中,我们常见的Debug方式很多中,例如最常见的DLog
DLog的使用,DLog在DeBug模式下会输出信息,包括方法名,行数以及你想要输出的内容。定义如下(包括ELog):
#ifdef DEBUG
#ifndef DLog
# define DLog(fmt, ...) {NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);}
#endif
#ifndef ELog
# define ELog(err) {if(err) DLog(@"%@", err)}
#endif
#else
#ifndef DLog
# define DLog(...)
#endif
#ifndef ELog
# define ELog(err)
#endif
#endif
那如何在Swift 中也能实现类似的功能呢?
最简单的方式采用以下方式:
#if DEBUG
println()
#endif
打印详细一些可以采用下面这种:
在Build-Setting中添加
class DLog {
func dLog(message: String, function: String = __FUNCTION__) {
#if DEBUG
println("\(function): \(message)")
#endif
}
}