【发布时间】:2020-09-07 03:16:35
【问题描述】:
我的 ios 应用程序上有 data.plist 文件,我用它来存储用户首选项。我可以阅读文件。但我无法写入文件,并且在捕获时遇到此错误。
注意:当我在测试设备上运行应用程序时会发生错误
Error Domain=NSCocoaErrorDomain Code=513 "你没有权限 将文件“data.plist”保存在“MyApp”文件夹中。” UserInfo={NSFilePath=/private/var/containers/Bundle/Application/37FE297E-5801-4681-AA7C-6FCF9BAA9E87/MyApp.app/data.plist, NSUnderlyingError=0x283996880 {错误域=NSPOSIXErrorDomain 代码=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)
}
}
}
【问题讨论】: