What is Plist file in detail? - ios

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?

PList is a property list.
You can find more useful information at:
https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html
http://nscookbook.com/2013/02/ios-programming-recipe-13-using-property-lists-plists/
How to use pList in iOS Programming
And the following one would give you more infor about the specific keys:
https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Introduction/Introduction.html

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:
A plist with the nice animation in Xcode looks like:
And finally a plist in binary looks like:
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!!

Related

How to merge multiple plist files into one?

Just to start, I really have no idea what Im doing. I was given this task for an internship, and am really learning as I go. I have multiple plist files, they consist of around 22 items each, and list values of colors. I need to merge all of these files into one, and am really not sure how to go about it. I have a certain structure I need to go by, and really Im not sure how to go about it. I was told to open the plists in texteditor and then paste all of the raw code into one text file, this doesn't seem to work as I only end up getting the values for the first plist I pasted into the text file. Any help would be nice. Thanks.
Assume your from.plist contains keys 1, 2 and to.plist contains 2, 3
Run this:
/usr/libexec/PlistBuddy -x -c "Merge from.plist" to.plist
to.plist will contain 1, 2, 3
There are a number of ways to handle this. By default a plist is a special form of XML file. If you figure out the syntax you can in fact use a text editor to merge the contents of multiple files together, but you need to make sure you get it right.
A plist file has a specific header for the entire file. You could not just copy/paste multiple plists together because then they would have that header repeated.
The next way to do it is programmatically. If you can figure out the type of outer collection these files contain (probably an array or a dictionary) then you could write a few lines of code that read in each of the plists as arrays, combines them using NSArray code (assuming they contain arrays of colors) and then save the combined array back to a new plist. As vadian says you can also use the NSPropertyListSerialization class. Thats a more general-pupose way of handling plist files, but it's also more complex and harder to figure out.
A third way to do it is in Xcode. If you right-click on a plist file and select "open in Xcode" it should give you Xcode's property list editor. You can then copy and paste the contents of the files together and save the results to a new file.
I figured it out!! First create the structure, or use the template given to you. I suggest opening this template/ structure in Xcode, as it makes it easier to switch between viewing the list as a plist and source code. Open your template as a source code. Then open each of your plists in text editor, and copy and paste the code from your plists into the appropriate area in your templates source code, then you can view it in Xcode as a property list to make sure it's correct. The only thing you have to be careful about here is making sure you are getting no errors. Otherwise this works great!!

replacing NSDictionary with text file

We have a software that is in objective c, its running on the mac, but the question is for iOS devs as well.
the software is reading many values from NSDictionary like this :
[self.dic setObject:#"307146" forKey:#"somename-PHOTOGRAPHY"];
it than takes the object, and the key before the - and after the - ,and use it .
We would like to replace the dictionary with .txt(or similar)which will be outside the app, and each row will have a data structure similar to the line here,so we could read it , and have as little changes in code from the current implementation .
Any example of code to create and read lines from a txt would be great.
What you probably want is a plist file.
This is an xml file that can be edited using a number of different apps on a Mac. Also text edit (as long as you get the format right).
You can then load the file and it will create an NSDictionary for you that you can use without any code changes.

Write NSArray to file on computer once

I've got a large amount of data stored in a CSV file. I can parse the file into a NSArray, but the process takes about 1 minute on the phone. I can then save this file to the phone using
[NSArray writeToFile:filePath atomically:YES]
Is it possible for me to save this NSArray to a file on the computer, and then only have to pull the NSArray from a file in the appBundle rather than have to parse it on the phone?
In other words, I have a massive CSV file that I want to convert to a file in the appBundle than can quickly be turned into an NSArray. I don't want the app to go out with the need for this file to be parsed at all. So how do I make a file that is readable by [NSArray initWithContentsOfFile:] from my CSV?
Yes you can, with some restrictions:
In iOS, the app bundle is read-only. You could build up this array and save it to your bundle as part of creating your app, but you could not do that at runtime.
I sometimes write code that creates files in the documents directory in the simulator, then manually copy the files out of the simulator's sandbox directory for my app and into the project.
If you want to build an array and save it to disk at runtime, you will need to save it to one of the allowed directories in your sandbox - probably the documents directory.
Saving your array to a file will only work if the entire "object graph" of objects contained in the array are all "property list objects" (NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary objects). You can have an array of dictionaries of arrays wit strings, numbers, data, dates, etc, in any combination, but if there is even 1 object down inside one of the other objects, the save will fail.
Another point:
The NSArray method writeToFile:atomically: converts your array to an XML property list file. Note that XML property lists are bigger and slower to save and load than binary property list files.
Take a look at the NSPropertyListSerialization class reference for info on saving an object to a binary property list, which is more compact and faster.
With a Mac program, possibly a target in the iOS app you are writing:
Parse the CSV to an NSArray (you state you can do this)
Save the NSArray to a file from a Mac program using
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
Add it as a resources in Xcode. Load it from the app bundle with
+ (id)arrayWithContentsOfFile:(NSString *)aPath

duplicate plist in xcode causes all values to be strings?

I am using plists to store preset settings for my iOS app. I've created a default plist that I would like to copy and use as a template for all of my presets. However, when I duplicate the plist in Xcode, all of the types become strings! Any ideas why this happens or how to fix this?
As an example, my default.plist looks like this in Xcode
-mono boolean NO
-name string default
-portamento number 0.001
and after i duplicate it into a new file:
-mono string NO
-name string default
-portamento string 0.001
Thanks!!
//////Thanks Michael Dautermann for the answer to this. When I created the .plist file in Xcode, I had appended .plist manually. This resulted in a file that looked great in Xcode, but wasn't actually an .xml file. As a result, the datatypes were not included in the source code. So, simply create a .plist file (and don't append .plist) and all is well.

Different Property List format on IOS

I'm facing an issue in using Property List. I've downloaded an example and the PList format is the following:
and using this code everything works fine:
NSString *path = [[NSBundle mainBundle] pathForResource:#"content_iPhone" ofType:#"plist"];
contentList = [NSArray arrayWithContentsOfFile:path];
Now, I'm trying to define a new PList by myself: I've created the new PList file in Xcode, and I've filled it on the User interface (following the previous plist structure), adding three dictionary items:
In this case, the same code doesn't work and analyzing the xml I see the following:
It seems that this is a dictionary of items instead of an array, as the first file.
Why? Can you please help me to create a correct property list?
Thanks in advance,
yassa
Use a text editor, change the first <dict> to <array>, change the last </dict> to </array>, delete all lines beginning <key>Item

Resources