Editing one of many dict's in plist file? - ios

On my root view controller (UITableViewController), I've read into NSMutableDictionary the root dict from the plist file and have displayed in the table, keys for all dict's within the root dict. When the user taps on any of the key, I pass the dictionary (again as an NSMutableDictionary) to that key to a new detail view controller where this dictionary is displayed.
Now when a particular detail of this dictionary is edited I would like to update the dictionary object and save to the plist file.
1- I'm not even able to update the NSMutableDictionary object e.g. using its - (void)setObject:(id)anObject forKey:(id)aKey method.
2- Is it possible to read into an NSMutableDictionary a plist file in the order as it is in the plist file because I would want to allow the user to re-arrange their views (hence update plist file accordingly).
XML structure:
<plist version="1.0">
<dict>
<key>abc</key>
<dict><key></key><string></string></dict>
<key>ijk</key>
<dict><key></key><string></string></dict>
</dict>
</plist>
Help will be really appreciated!
Thanks
Bilal.

To answer part 2 of the question, a dictionary is the wrong data structure to use when you want to maintain a particular ordering of the contents. A dictionary is a key-value structure, not an ordered collection. You may want an NSMutableArray instead.

Related

How to show dictionary values on clicking a dictionary type row in UITableView?

My problem is, that I am fetching a PLIST which consists of String, boolean and Dictionary type data. My aim is to construct such a UI in which I am able to fetch this PLIST edit it and save the update back in the PLIST. So far, I am successful in fetching and saving the updates for string and boolean type, but I want that when I click on my Dictionary Type row, the keys of that dictionary should be populated as rows in my tableview and clicking on this rows the value should appear.
So what is the best way to show this dictionary type keys (i.e by adding the new rows on row click event or showing another tableview with new values of that dictionary?) or is there any other efficient way which I am missing.
You can use KOTree, which support multi-level data view support. It's for sure, you have to modify the KOTreeTableViewCell for your own plist related editing support.
here is a sample as which showing the item hierarchy, as it can be in your plist file format.

What is the need to store the Content of plist file into an NSArray?

I am learning Objective-C language. I have covered the following questions:
What is plist?
How to create a plist file?
What are the representation of plist file?
But I did not find the answer of these questions:
Why do we store a plist in NSArray?
What is the need to store a plist into NSArray?
Can you please help me to clear my doubts?
Sometime you want to do something at runtime or create something base on something else.
Let's imagine you have a game with a different levels and you want the levels to be vary you can hard code the levels in code but much nicer would be store it in the file. iOS lets you very easily load plist to array or dictionary and that's the reason why iOS developers choose to use it.
// Example of plist file
Level1:
NumberOfEnemies: 6
ScoreTarget: 100
....
Level2:
NumberOfEnemies: 12
ScoreTarget: 120
....
When you load the game you can read the plist and you can load the level base on the content.
It's much easier to add another level or add more customisation to the plist (file) that go back to the code any do it there.
It's just one of the examples but you could use plist to do much more.
Plist are mostly configuration files or place to store some values that you know that will never change. One particular example of the plist file is info.plist where you define attributes for the application.
Plist are nothing else but the same old XML file with attributes and elements. That said, you actually access plist through dictionary, not array.
For your question why do we store it in dictionary, you use dictionary to access these values since there is no other way to extract the information from it.
One more thing, plists have their own restrictions regarding the type you can store: Array, Dictionary, String, Date, Data, Number and Boolean.
PList : is a "Property List" file, uses the XML format to store objects such as arrays, dictionaries, String, Data, Number and Boolean.
How to create :
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:#"myPlistFile.plist"];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#”myPlistFile” ofType:#”plist”];
[fileManager copyItemAtPath:bundle toPath:path error:&error];
}
[myDict writeToFile:plistPath atomically: YES];
Representation : Based on what is stored in plist file .
Key and Value. (NSDictionaries)
Key and multiple value . (NSArray) etc..
What is PLIST?
Plist stands for Property List. Plist is used to store data in hierarchical manner.A Property List can contain containers and primitives.
Containers – Array, Dictionary
Primitives – Boolean, String, Number(Integer,Float), Date, Data
A Container can contain other containers and primitive data-types.
i.e. Dictionary can contain other Dictionaries,Arrays and Primitives. Array can contain other Dictionaries,Arrays,Primitives.
How to Create PLIST File?
STEP 1: Right Click the Project
STEP 2: Choose New File
STEP 3: Select Resource from left side of the Template.
STEP 4: Then Click Property List
STEP 5: Click NEXT button of the Bottom.
STEP 6: Give property List Name.
STEP 7: Finally Click OK
For more Reference please go through the below link
https://stackoverflow.com/questions/9044735/steps-to-create-and-edit-a-plist-file-in-xcode
What are the Representation of PLIST file?
A property list can be stored in one of three different ways:
1. in an XML representation,
2. in a binary format, or
3. in an “old-style” ASCII format inherited from OpenStep.
You can serialize property lists in the XML and binary formats. The serialization API with the old-style format is read-only
XML property lists are more portable than the binary alternative and can be manually edited, but binary property lists are much more compact; as a result, they require less memory and can be read and written much faster than XML property lists. In general, if your property list is relatively small, the benefits of XML property lists outweigh the I/O speed and compactness that comes with binary property lists. If you have a large data set, binary property lists, keyed archives, or custom data formats are a better solution.
I refer the link for Why do we store a plist in NSArray? and What is the need to store a plist into NSArray?
1.http://hayageek.com/plist-tutorial/
2.https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html
3.http://nscookbook.com/2013/02/ios-programming-recipe-13-using-property-lists-plists/
4.Getting plist data in a order

create and update plist (array of dictionaries)

I have a orderedItems.plist file. I want it to have an array of dictionaries.
Here's a sample:
{
iItemDayPriceId: 72
fQty: 1
sItemName: "Tomato Juice"
fPrice: 10
sModifier: "test modifier"
}
I want to save it in a plist. Then, for example the user will order another item. I will setup the dictionary again and add it to the plist file.
So here's what I'm thinking to resolve this:
Loop through the array of dictionaries in the plist file.
Check if iItemDayPriceId inside the dictionary is already existing.
If existing, update the dictionary with iItemDayPriceId.
If not existing, add the new array of dictionary to the existing plist file.
May I know how can I do this?
Thank you so much!

Plist writeToFile overwrites the entire plist

I have been successfully using a Plist to read and write data in my iPhone app. The Plist consists of dictionaries in arrays. When I want to append a dictionary I use the writeToFile method. This works well but it overWrites the entire Plist. Is there way to add to a dictionary without having to read the whole Plist and then right the whole Plist back again ??
Pete, if I understand your question correctly, you should do the following:
Load your plist file in an NSMutableArray. Then, when you want to add another dictionary, instead of saving it directly (it will of course overwrite...), you just add this dictionary to your existing NSMutableArray and then you save it.
NSMutableArray *arrayLoaded = <load here your plist>
NSDictionary *dictionaryToAdd = <create your dictionary>
[arrayLoaded addObject:dictionaryToAdd];
[arrayLoaded writeToFile:filePath];

iOS - NSDictionary - Reading plist file

I've a plist file which has a menu to be shown (related to a restaurant)
I added the dictionary items in the order they have to be shown.
But when I'm reading the plist file into NSDictionary, the same order in plist is not being maintained. Why? How should I display it in the same order as that in the plist?
Instead of dictionary, we've to create nsarray which will read same and so we can display the same.

Resources