ios - 如何将控制台打印语句保存到 Xcode 中的文本文件?
问题描述
如何将控制台打印语句保存到 Xcode 中的导出文本文件?我想将我最后一个控制台打印的语句保存在我计算机中导出的日志文本文件中。
解决方案
您可以使用如下函数:
func write(text: String, to fileNamed: String, folder: String = "SavedFiles") {
guard let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else { return }
guard let writePath = NSURL(fileURLWithPath: path).appendingPathComponent(folder) else { return }
try? FileManager.default.createDirectory(atPath: writePath.path, withIntermediateDirectories: true)
let file = writePath.appendingPathComponent(fileNamed + ".txt")
try? text.write(to: file, atomically: false, encoding: String.Encoding.utf8)
}
并在命令行中搜索您指定的writePath
变量将为您提供的文件。如果需要,您可以将该文件复制到其他地方
推荐阅读
- c# - 有没有办法避免使用 EF Core 查询进行 foreach
- mysql - 全文搜索未找到某些特定词,这些词未包含在停用词中
- java - 如何检查java字符串是否包含数字和字母?
- python - Python套接字:接收帧队列管理
- python - 测试字符串长度是奇数还是偶数的函数
- jestjs - 与 esm 模块或 commonjs 一起使用的 jest 文件转换
- python - 在 Bokeh 中使用 hovertool 时仅显示一个工具提示
- amazon-web-services - AWS-Amplify project-config.json' 不存在错误
- react-native - 错误:找不到默认重定向 url。如果您使用裸工作流程,则需要自己提供
- flutter - Flutter 可拖动的小部件,可以单击或拖动以移动到 DragTarget