print只在 PRODUCT 环境下才打印log
func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
#if !PRODUCT
let output = items.map { "\($0)" }.joined(separator: separator)
Swift.print(output, terminator: terminator)
#endif
}
自定义打印log的方法
func printLog(_ items: Any..., separator: String = " ", terminator: String = "\n", file: String = #file, funcName: String = #function, line: Int = #line) {
#if DEBUG || UAT
let fileName = (file as NSString).lastPathComponent
let output = items.map { "\($0)" }.joined(separator: separator)
let dateString = StringFormatterUtils.dateToString(Date(), dateFormat: "yyyy/MM/dd HH:mm:ss")
Swift.print("[\(dateString)][\(fileName)][\(funcName)][Line-\(line)] \(output)", terminator: terminator)
#endif
}
struct StringFormatterUtils {
static func stringToDate(_ string: String, dateFormat: String = "yyyyMMdd") -> Date? {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(identifier: "Asia/Tokyo")
formatter.dateFormat = dateFormat
let date = formatter.date(from: string)
return date
}
static func dateToString(_ date: Date, dateFormat: String = "yyyy/MM/dd") -> String {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(identifier: "Asia/Tokyo")
formatter.dateFormat = dateFormat
let date = formatter.string(from: date)
return date
}
static func stringToDateString(_ string: String, fromFormat: String = "yyyyMMdd", toFormat: String = "yyyy/MM/dd") -> String {
let fromFormatter = DateFormatter()
fromFormatter.locale = Locale(identifier: "en_US_POSIX")
fromFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo")
fromFormatter.dateFormat = fromFormat
let date = fromFormatter.date(from: string)
let toFormatter = DateFormatter()
toFormatter.locale = Locale(identifier: "en_US_POSIX")
toFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo")
toFormatter.dateFormat = toFormat
let dateStr = toFormatter.string(from: date ?? Date())
return dateStr
}
}