我正在尝试写入/读取包,并使用 StackOverflow 中的以下参考:
看起来不错的代码,应该可以正常工作。
if let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last {
let fileURL = documentsDirectory.appendingPathComponent("file.txt")
do {
if try fileURL.checkResourceIsReachable() {
print("file exist")
} else {
print("file doesnt exist")
do {
try Data().write(to: fileURL)
} catch {
print("an error happened while creating the file")
}
}
} catch {
print("an error happened while checking for the file")
}
}
获取文件路径:
var filePath = Bundle.main.url(forResource: "file", withExtension: "txt")
我可以保存文件,但不知何故我永远找不到文件路径
var filePath = Bundle.main.url(forResource: "file", withExtension: "txt")
我无法获得任何扩展。我什至可以在 XCode 中的设备下的应用程序数据中看到保存的文件,这看起来很棒,但我找不到它filePath。你能帮我么?