Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS Swift:将打印和调试打印写入文件_Ios_Swift_Xcode_Debugging_Printing - Fatal编程技术网

iOS Swift:将打印和调试打印写入文件

iOS Swift:将打印和调试打印写入文件,ios,swift,xcode,debugging,printing,Ios,Swift,Xcode,Debugging,Printing,晚上,是否可以将所有打印和调试打印保存在一个文件中 我希望有我的应用程序的日志,即使它不是从Xcode启动的 我想重写print和debugPrint方法,并将输入写入一个文件 谢谢可以在iOS中将所有打印/调试打印日志写入文件。使用以下命令将调试日志写入文件 注意:如果使用以下代码,则不会在控制台上打印日志 func writeIntoFile() { if let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(

晚上,是否可以将所有打印和调试打印保存在一个文件中

我希望有我的应用程序的日志,即使它不是从Xcode启动的

我想重写print和debugPrint方法,并将输入写入一个文件


谢谢

可以在iOS中将所有打印/调试打印日志写入文件。使用以下命令将调试日志写入文件

注意:如果使用以下代码,则不会在控制台上打印日志

func writeIntoFile() {
    if let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
        let filePath = documentDirectoryPath + "/logfile.txt"
        freopen(filePath.cString(using: .ascii), "a", stderr)
    }
}

Swift标准库中有以下方法:

func print<Target>(_ items: Any..., separator: String = default, terminator: String = default, to output: inout Target) where Target : TextOutputStream
然后

// I would probably use Singleton for this
var dest = LogDestination()
print("My test message", to: &dest)

我认为您希望将日志保存到外部文件中。 如果是,那么您可以尝试以下方法:

func redirectLogs(flag:Bool)  {
    if flag {
        if let documentsPathString = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
            let logPath = documentsPathString.appending("app.log")
            freopen(logPath.cString(using: String.Encoding.ascii), "a+",stderr)
        }
    }
}

可能有帮助:,请检查仍不清楚mo如何使用它。。你能给我举个简单的例子吗?我已经添加了example@mattt我最近就这个话题写了一篇文章。读得好,我想,这在xcode 12.4中不起作用。我喜欢苹果科技。不过,如果他们能停止对swift进行更改,我将不胜感激。苹果???你好
// I would probably use Singleton for this
var dest = LogDestination()
print("My test message", to: &dest)
func redirectLogs(flag:Bool)  {
    if flag {
        if let documentsPathString = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
            let logPath = documentsPathString.appending("app.log")
            freopen(logPath.cString(using: String.Encoding.ascii), "a+",stderr)
        }
    }
}