【问题标题】:can't write to data.plist file on IOS无法写入 IOS 上的 data.plist 文件
【发布时间】: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)
        }
    }
}

【问题讨论】:

    标签: ios swift plist


    【解决方案1】:

    您无权将“data.plist”文件保存在“MyApp”文件夹中

    您的应用程序包是只读的。将文件保存在可写目录之一中,例如文档或应用支持目录。

    使用 Filemanager.url(for:in:appropriateFor:create:) 获取其中一个目录的 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      • 1970-01-01
      • 2011-03-25
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多