首页 > 解决方案 > 如何将控制台打印语句保存到 Xcode 中的文本文件?

问题描述

如何将控制台打印语句保存到 Xcode 中的导出文本文件?我想将我最后一个控制台打印的语句保存在我计算机中导出的日志文本文件中。

标签: iosswiftxcodedebuggingprinting

解决方案


您可以使用如下函数:

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变量将为您提供的文件。如果需要,您可以将该文件复制到其他地方


推荐阅读