Liu2er
Liu2er

Reputation: 11

What is Plist file in detail?

I am learning iOS. I have a confusion about the Plist file, about the creator, applicable platform, format and so on. So what the Plist file really is?

Upvotes: 0

Views: 1537

Answers (2)

Michael Mooney
Michael Mooney

Reputation: 347

A plist file is a property list. You can either create it using the nice animations Xcode gives you, or you can create pragmatically using XML. A plist file is something that can store objects (string, bool, data, date, number), like a database. And you can run through the plist file to retrieve or store the information just like a database.

In games you mostly save your score using NSUserDefaults as the data isn't sensitive, however saving information like a home address in NSUserDefaults isn't the best idea. Instead you'd rather want to save the information in a database - a plist file. Apple uses plist files in their apps. When you open contacts the information is retrieved from the plist and then put into a UITableView. When you click on a person it gives you their details, the details which were received from the plist file.

Another great things about a plist file is that you can change it from binary to XML and vice versa. Why would you ever want to change it to binary? Sometimes when you're dealing with large data e.g. a whole dictionary, it'll be faster to run through the data is binary than it would be in XML. To change it into binary, you go to terminal and use this command, plutil -convert binary1 yourFile.plist. To change binary to XML you use this command, plutil -convert xml1 yourFile.plist.

A plist in raw XML looks like:

enter image description here

A plist with the nice animation in Xcode looks like:

enter image description here

And finally a plist in binary looks like:

enter image description here

Now lets say you've created your plist and stored all the information in it that you want. To retrieve this information (in objc) use the following code.

NSString *path;
    path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"plist"];
    NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
    for (NSString *str in array) {
        @autoreleasepool {
            NSLog(@"%@", str);
        }

    }

Hope this helped you!!

Upvotes: 0

Related Questions