[英]can't write to data.plist file on IOS
我的 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 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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.