Take the 2-minute tour ×

Why use plists and xml files? If I only want to store a few values, is it okay to use plain text files or does this go against Objective-C best practices?


I'm not sure if this should go in a separate post or not, so I'll just put it here...

If I'm making an app where a user can design a cupcake and save it with their preferences (color, flavor, size), which method should I use. I imagine my users aren't going to make hundreds of designs, but some will inevitably make a large number.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

The main reason that plists are commonly used is because the native APIs can handle these easily. You can load a NSArray/NSDictionary directly from a plist with one command.

SQL databases are used when you are going to have many occurrences of similar data. For example, if you need to record contacts for a social app, you would use a database that could contain the id, name, age, gender, phone number, email, etc.

Other than these, there are custom binary formats, but these are specialized for whatever project is being worked on. Depending on what you need to accomplish, a text file could work for you, but there may be better answers. There is nothing wrong with using text files, but these are not commonly used as you would have to write your own methods to parse them. Really, I would need to know more about what type of data you will be storing before I could tell you which option would be best.

An edit to answer your edit:

For this, the best thing to do would be to use a SQL database, as every cupcake is going to have various properties, such as name, type of cake mix used, type of frosting, color of frosting, sprinkles yes/no, etc. This is perfect for a SQL database, because dbs have named colums (ie "name", "mixType", etc), and each row in the table will have different values for each of these columns.

This could also be implemented with a plist, but it wouldn't be as efficient, and it would use more disk space (although not much if youre just using it for cupcakes). I assume that you have a Cupcake class, so you could just implement a load function like this:

+(id)cupcakeWithContentsOfFile:(NSString*)file {
    if((self = [super init])) {
        NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile:file];
        self.flavor = [plist objectForKey:@"flavor"];
        // Etc.
    return self;
share|improve this answer

I'd say it's fine, but considering how easy they make it to read plist files, my question would be why bother?

share|improve this answer
It's also very easy to create text files with NSFileManager and NSFileHandler. But that's a good point. –  pasawaya Jun 1 '12 at 21:21
but then you have to read in and write the files line by line while parsing. why do any extra work at all when it buys you nothing? –  Jarsen Jun 1 '12 at 21:31

This is an interesting question. I actually have an app out there that DOES indeed use simple text files for data that the app uses. Because the original code came from a windows / mac program, and I wanted to keep the data files consistent, Windows doesnt provide pfile type operation. I wrote all of the code on windows to read in the files, and since it was all ANSI-C it transported to MacOS X quite nicely.

When it came to porting it over to the iPhone and iPad, it still was just as easy to port the code then to re-write not only the data files into PList format, but also the PList reading code.

For All apps that I have begun from scratch that arent windows bound also, I have used PLists though.

This is actually an issue that I go back and forth on. PList and / or XML reading and processing is certainly NOT going to be as high of performance as a well designed text file format as both are bloated due to all of the excess tags that may or may not be necessary. If you are trying to develope a single data file that has many types of data, than this could be the simpler approach, but if you are looking for less datasize, and maybe faster execution, then formatting your own file may be much better. I know this isnt a definitive answer.

So in the end, it isnt a "best practices" sort of thing, but really a preference type of thing.

share|improve this answer

Your Answer


