【发布时间】:2021-03-26 18:45:13
【问题描述】:
最近,我创建了两个 Swift 函数来覆盖 Swift 标准库中的 print(...) 和 debugPrint(...)。我把这两个函数放在了项目范围内。
func debugPrint(_ items: Any..., separator: Swift.String = " ", terminator: Swift.String = "\n") -> ()
{
#if DEBUG
typealias newDebugPrint = (_ : [Any], _ : Swift.String, _ : Swift.String) -> ()
let castedDebugPrint = unsafeBitCast(Swift.debugPrint, to: newDebugPrint.self)
castedDebugPrint(items, separator, terminator)
#else
// Do nothing...
#endif
}
func print(_ items: Any..., separator: Swift.String = " ", terminator: Swift.String = "\n") -> ()
{
#if DEBUG
typealias newPrint = (_ : [Any], _ : Swift.String, _ : Swift.String) -> ()
let castedPrint = unsafeBitCast(Swift.print, to: newPrint.self)
castedPrint(items, separator, terminator)
#else
// Do nothing...
#endif
}
使用上面的函数可以让我们使用originprint(...)和debugPrint(...),并且不用担心发布构建时会输出大量消息。但是,它们在发布版本中使用真的安全吗?想知道此覆盖背后的任何潜在风险吗?
任何想法将不胜感激!
【问题讨论】: