我的ios应用程序上有data.plist文件,我用它来存储用户首选项。我可以读取文件。但是我无法写入文件,但是我却发现了这个错误。
注意:当我在测试设备上运行应用程序时发生错误
Error Domain = NSCocoaErrorDomain代码= 513“您无权将文件“ data.plist”保存在“ MyApp”文件夹中。”UserInfo = {NSFilePath = / private / var / containers / Bundle / Application / 37FE297E-5801-4681-AA7C-6FCF9BAA9E87 / MyApp.app / data.plist,NSUnderlyingError = 0x283996880 {Error Domain = NSPOSIXErrorDomain Code = 1“不允许操作”}}
func updateDataPlist(){
self.thresholdForAdminBalances = Int(self.txtfThresholdForAdminBalances.text!)
self.thresholdForSubUserbalances = Int(self.txtfThresholdForSubUserBalances.text!)
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
let preferences = Preferences(thresholdForAdminBalances: thresholdForAdminBalances!, thresholdForSubUserBalances: thresholdForSubUserbalances!)
if let path = Bundle.main.url(forResource: "data", withExtension: "plist"){
do {
let data = try encoder.encode(preferences)
try data.write(to: path)
} catch {
print(error)
}
}
}
您无权将文件“ data.plist”保存在文件夹“ MyApp”中
您的应用捆绑包是只读的。将文件保存在其中一个可写目录中,例如文档或应用程序支持目录。
使用Filemanager.url(for:in:appropriateFor:create:)
获取这些目录之一的URL。