create and update plist (array of dictionaries) - ios

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!

Related

Data structure that allows you to add multiple objects for the same key

I am looking for a data structure that allows me to add multiple objects for the same key.
I have a table that is downloading images and when it finishes downloading an image I want it to set the image in all the cells with that image name.
So I want a data structure where the key is the image name and it multiple objects i.e pointers to the cells.
I know I can't do this in NSMutable dictionary as it just overwrites the keys. Is there another way?
Simply create a NSDictionary containing a NSMutableArray :
NSDictionary *myDico = #{#"imageName":[NSArray arrayWithObjects:...]};
Where ... = the references to the cells.
Edit :
You also can, if you have multiple images, use a NSMutableDictionary.
How about using NSMutableDictionary whose value is a NSMutableArray? You can append the array when new cells are added or check nil and add an empty array when the key is not stored before.
Just use NSArray to add multiple values against single key.
NSDictionary *dictt = #{#"imageKey":#[#"image1", #"image2"]};

Objective C: Print/Log an array as usable code?

I'm programmatically building an array of dictionaries with hundreds of values I'm pulling from various sources. I then plan to go in and manually change a few of them as needed. I don't want to use this pulling method in the production code, I want to just store it as a vanilla array with dictionaries. However when using NSLog it returns the array obviously, but not as code I can copy and paste. I'd hate to have to go through several hundred values and and manually convert everything to be proper objective c code.
So in short, is there any way to return my array as actual code?
One approach would be to write the final array to a plist file. Then make the plist file part of your project. No need to generate code for the array. Simply load the plist into an array at runtime.
To save the array as a plist:
[myFinalArray writeToFile:somePath atomically:YES];

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.

Editing one of many dict's in plist file?

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.

Resources