0

beginner in Swift here.. I've already tried to look it up, but couldn't find an answer that worked.

I'm trying to write a macOS app, and I would like to know how to edit a plist file programmatically that is on the system but not bundled in the app.

Let's say, for example, we have a "contents.plist" file which is located in a folder in the system, and I want to access this file, then add keys and dictionaries to it, and save the changes in that file.

Any idea on how can I do that programmatically with swift ?

Thank you !

EDIT : PropertyListSerialization worked ! Thank you vadian what I did to modify my external plist was to define a path in a constant and access it using FileManager.default.contents(atPath: myPath) instead of Bundle.main.path ..

Then use .add methods and .write to edit the plist .

Jnios
  • 11
  • 3
  • Use `PropertyListSerialization`. It’s the recommended API to read and write plist files in Swift (even in Objective-C). – vadian Jan 23 '20 at 09:25
  • Thank you for your quick response, I've went through the documentation on apple's developer website but can't figure out how to practically use it.. Would you have an exemple on how to apply it on my case ? (How to retrieve path/read/write on the plist) ? – Jnios Jan 23 '20 at 09:36
  • Please see for example https://stackoverflow.com/questions/40436895/how-to-read-plist-without-using-nsdictionary-in-swift. Rather than the URL in the bundle pass the URL in the file system. – vadian Jan 23 '20 at 09:38
  • What specific problems are you encountering? You must have tried something? – Magnas Jan 23 '20 at 10:15
  • So in the link provided, instead of using "if let url = Bundle.main.url(forRessource:"contents", withExtension: "plist") , I should use something like let url = FileManager.default.contents(atPath: pathtomyplist) to access the plist ? – Jnios Jan 23 '20 at 10:28
  • It's highly recommended to use always the URL related API (rather than string paths) – vadian Jan 23 '20 at 12:07

0 Answers0