如何在 swift 中将数组保存到应用程序主bundle中的 .plist



如何使用数组类型将数组保存到 .plist?

我试过这个,但不起作用

    let path = NSBundle.mainBundle().pathForResource("Setting", ofType: "plist")
    let arrayWithProperties = NSArray(array: [
        NSNumber(integer: nowThemeSelected),
        NSNumber(integer: imageForBackgroundNumber),
        NSNumber(integer: imageForButtonNumber),
        NSNumber(integer: colorOfButton)])

    arrayWithProperties.writeToFile(path!, atomically: true)

您无法写入 iOS 中的主捆绑包,应用程序捆绑包中的所有内容都是只读的。

[应用程序的捆绑包]目录包含该应用程序和所有 其资源。不能写入此目录。为了防止 篡改时,捆绑目录在安装时签名。 写入此目录会更改签名并阻止你的应用 从启动。但是,您可以获得对任何 存储在应用程序捆绑包中的资源。

请考虑阅读文件系统编程指南,然后将 plist 保存到正确的位置。最好是文档目录或库目录。轮到你了。

您不能写入捆绑目录,但可以写入文档目录。所以,请使用这个代码!

let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
var path:NSString = documentsPath.stringByAppendingPathComponent("Setting.plist");

当你写到文件()时,请使用这个:

arrayWithProperties.writeToFile(path as String, atomically: true)

祝你好运!

相关内容

最新更新