gpt4 book ai didi

ios - 快速写入文件

转载 作者:行者123 更新时间:2023-11-28 06:47:11 25 4
gpt4 key购买 nike

这是我在 appDelegate didFinishLaunchingWithOptions() 中的代码

  var paths : NSArray =  NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var documentsDirectory = paths.objectAtIndex(0)
var fileName: String = String(format: "Logger.txt")
var logFilePath : NSString = documentsDirectory.stringByAppendingPathComponent(fileName)
freopen(logFilePath, "a+", stderr)

我得到的错误是无法将 NSString 转换为 UnSafepointer。谁能帮我实现这个?

最佳答案

只需从 logFilePath 中删除 NSString:

var paths : NSArray =  NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var documentsDirectory = paths.objectAtIndex(0)
var fileName: String = String(format: "Logger.txt")
var logFilePath = documentsDirectory.stringByAppendingPathComponent(fileName)
freopen(logFilePath, "a+", stderr)

更安全的方法是这样的:

let file = "Logger.txt"
let text = "A safer way to do this"

if let directory : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
let path = directory.stringByAppendingPathComponent(file);

print(path)

do {
try text.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding)
}
catch {
}
}

关于ios - 快速写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35980055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com