TVOS FileManager 的 createFile 失败并出现错误 1
TVOS FileManager's createFile fails with error 1
我正在尝试在 Apple TV 设备上创建一个 json 文件以保存一些数据,但 createFile(...) 总是失败,返回 false。我试过绝对路径、相对路径和路径都没有成功。 jsonData
变量在我的实现中创建得很好,它在模拟器上工作:
self.fileName = "MyFileTest"
self.directory = .documentDirectory
let documentsDirectory = fileManager.urls(for: self.directory, in: .userDomainMask)[0]
self.fullyQualifiedPath = documentsDirectory.appendingPathComponent(self.fileName).appendingPathExtension("json").path
do {
let jsonData = try convertObjectToData(data: dataForJson)
if !fileManager.createFile(atPath: fullyQualifiedPath, contents: jsonData as Data, attributes: nil) {
print("File Manager failed at createFile")
throw FileErrors.FileNotSaved
}
} catch {
print("Unable to create json file \(error.localizedDescription)")
throw FileErrors.FileNotSaved
}
此处 createFile 失败,returns 错误,打印出以下内容:
File Manager failed at createFile Unable to create json file The
operation couldn’t be completed.
(TestAppTVOS.FileSaveHelper.(FileErrors in
_70D0A1275AC2AFFFA4ED048E3A809030) error 1.)
fullyQualifiedPath 变量值为:
/var/mobile/Containers/Data/Application/00DCB709-5EC6-40FC-BB21-D797EB4FE2F5/Documents/MyFileTest.json
不确定如何从错误消息 "The operation couldn’t be completed" 和 "error 1" 中得出什么结论?有什么想法可以让 Swift 3 正常工作吗?
在这上面花了太多时间后,我发现它似乎与我在哪个文件夹中创建文件有关。
由于我在沙盒中 mode/debugging 我无法写入 Documents 文件夹,因此不得不切换到 Cache 文件夹。
例如
self.directory = .cachesDirectory
现在有效。
我正在尝试在 Apple TV 设备上创建一个 json 文件以保存一些数据,但 createFile(...) 总是失败,返回 false。我试过绝对路径、相对路径和路径都没有成功。 jsonData
变量在我的实现中创建得很好,它在模拟器上工作:
self.fileName = "MyFileTest"
self.directory = .documentDirectory
let documentsDirectory = fileManager.urls(for: self.directory, in: .userDomainMask)[0]
self.fullyQualifiedPath = documentsDirectory.appendingPathComponent(self.fileName).appendingPathExtension("json").path
do {
let jsonData = try convertObjectToData(data: dataForJson)
if !fileManager.createFile(atPath: fullyQualifiedPath, contents: jsonData as Data, attributes: nil) {
print("File Manager failed at createFile")
throw FileErrors.FileNotSaved
}
} catch {
print("Unable to create json file \(error.localizedDescription)")
throw FileErrors.FileNotSaved
}
此处 createFile 失败,returns 错误,打印出以下内容:
File Manager failed at createFile Unable to create json file The operation couldn’t be completed. (TestAppTVOS.FileSaveHelper.(FileErrors in _70D0A1275AC2AFFFA4ED048E3A809030) error 1.)
fullyQualifiedPath 变量值为:
/var/mobile/Containers/Data/Application/00DCB709-5EC6-40FC-BB21-D797EB4FE2F5/Documents/MyFileTest.json
不确定如何从错误消息 "The operation couldn’t be completed" 和 "error 1" 中得出什么结论?有什么想法可以让 Swift 3 正常工作吗?
在这上面花了太多时间后,我发现它似乎与我在哪个文件夹中创建文件有关。
由于我在沙盒中 mode/debugging 我无法写入 Documents 文件夹,因此不得不切换到 Cache 文件夹。
例如
self.directory = .cachesDirectory
现在有效。