Plist writeToFile overwrites the entire plist - ios

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];

Related

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

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];

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!

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