ios - 在 Swift 中隐藏(跳过)print() 和 debugPrint() 的方法
问题描述
最近,我创建了两个 Swift 函数来覆盖print(...)
和debugPrint(...)
来自 Swift 标准库。我把这两个功能放在项目范围内。
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
}
使用上面的功能可以让我们使用origin print(...)
,debugPrint(...)
并且不用担心发布构建时会输出大量消息。但是,它们在发布版本中使用真的安全吗?想知道这个覆盖背后的任何潜在风险吗?
任何想法将不胜感激!
解决方案
你不需要做所有这些......这实际上是同一件事:
func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
#if DEBUG
items.forEach {
Swift.print($0, separator: separator, terminator: terminator)
}
#endif
}
您可能还想看看这个答案进行更多讨论:https ://stackoverflow.com/a/38335438/6257435
推荐阅读
- bash - `bash` 正在泄漏内存,我在哪里报告它?
- reactjs - 在 useEffect 挂钩中使用对象属性而不是对象作为依赖项是不好的做法吗?
- bootstrap-5 - Bootstrap 5:如何添加 $font-family-serif 变量
- typescript - 带有 Expo 和 Typescript 的 NPM 工作区
- android - 代码已粘贴,但它在 Android SSH 客户端上执行了相反的行顺序
- vba - VBA 用户表单组合框是 Outlook 联系人姓名列表和命令按钮,用于在选择姓名时搜索匹配的联系人家庭电话号码
- javascript - 以闪亮的方式删除html表中的特定行
- python - 如何使用用户输入迭代地构建 pd 系列,一次一个单元格?
- flutter - 从 Flutter 中的异步方法中调用一个确定的值。等待问题
- mysql - .ibd 文件在 ubuntu 服务器上占用太多空间