Bubu
Bubu

Reputation: 669

When to use plist?

I know what plist does but what is the general purpose of plist?

Is there a standard or Apple recommendation for when use it for reading / writing data with it?

Upvotes: 2

Views: 1515

Answers (3)

nikkumang
nikkumang

Reputation: 1635

Plists are convenient for when you need to store small amounts of persistent data. The API is simple. Large plist files are not recommended. Apple suggests less than a few hundred kilobytes. You would not use a plist for storing user generated data, since over time they might exceed that limit, only use it for data whose size you control.

Upvotes: 0

SagarU
SagarU

Reputation: 426

Think of a plist as a file-based implementation of a Dictionary (or NSDictionary). What you have inside are key-value pairs which you can parse and use as part of your logic. If you might have observed, there would be a Info.plist file in every project. It stores values for different configurations you might want to add. An example would be NSAppTransportSecurity

Once you have the values in the file, you can use it as:

    var configDict: NSDictionary?
if let path = NSBundle.mainBundle().pathForResource("Config", ofType: "plist") {
    configDict = NSDictionary(contentsOfFile: path)
}
if let dict = myDict {
    // Use configDict here
}

Keep in mind that you are not limited just to the default Info.plist that comes bundled with the project. You can create one of your own too. Consider the scenario where your are fetching a number of configurations at launch. You can save it as a plist and reference later.

Answering the second part of your question, reading and writing to a file, if performed at very short intervals, seems like a unnecessary overhead. Its better to use local Dictionary variables and then write at longer time intervals or when you are sure that changes are done. Plist is more of a kind of persistent storage. So you can opt to write to the file when the app enters background or user kills the app.

Note: Keep in mind that plist is essentially a plain file in your file system. There is nothing to prevent someone from reading it (and I know about sandboxing). That is why its wise to never store any passwords etc in a plist (nor in UserDefaults).

Upvotes: 1

matt
matt

Reputation: 535890

Suppose your app has a big constant that it uses, like the names of all the countries in the world in alphabetical order. That's an array of strings. How will you create that constant?

One way might be to type the whole array in code, a really big array.

It might be easier to configure this as a .plist file and read the file into an array as your app launches.

So, that is one use of a .plist file: it's a text rendering in a canonical format for data that you will need to use during the app's lifetime.

And of course the same thing works in reverse; you could save an array of strings as a .plist file while the app runs, in order to read it again the next time the app runs. (That in fact is how UserDefaults works.)

Upvotes: 3

Related Questions