Swift日志打印的正确姿势

打印日志是程序员调试代码的无上利器,俗话说“不会打印日志的开发不是好码农”。但在发布环境中,日志打印又会影响代码性能,带来不必要的开销。嗯哼,上线前一行行注释或删除调试代码的滋味好受吗?

只需两步,让你不再忧愁

  1. 设置Other Swift Flags DEBUG标志

    -D DEBUG

  2. 重写print & debugPrint

  func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
      #if DEBUG
          Swift.print(items[0], separator:separator, terminator: terminator)
      #endif
  }
func debugPrint(_ items: Any..., separator: String = " ", terminator: String = "\n") {
    #if DEBUG
        Swift.debugPrint(items[0], separator:separator, terminator: terminator)
    #endif
}

如果你要重写Swift 2.x的版本,请参考 stack overflow

你可能感兴趣的:(Swift日志打印的正确姿势)