【发布时间】:2019-09-28 17:07:42
【问题描述】:
我正在尝试写入/读取包,我使用 StackOverflow 中的以下参考:
How to access file included in app bundle in Swift?
看起来不错的代码,应该可以正常工作。
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 找到它。你能帮帮我吗?
【问题讨论】: