How to save an array to .plist in the App's mainBundle in swift

后端 未结 2 1109
梦谈多话
梦谈多话 2020-12-22 09:42

How I can save array to .plist with array type?

I try this, but is not working

    let path = NSBundle.mainBundle().pathForResource(\"Setting\", ofTy         


        
2条回答
  •  被撕碎了的回忆
    2020-12-22 10:25

    You can't write to the Bundle directory but you can write to the Documents directory. So, use this code!

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

    When you'll writeToFile() use this:

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

    Good Luck!

提交回复
热议问题