1

I have the following plist structure.I am really confused about what to do for writing the items into the plist without overwriting the existing data and reading them out.

The following is how my plist is,Please suggest me an appropriate coding logic for storing mechanism without overwriting the contents of it and how can i retrieve it later.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>PlacesID</key>
    <array/>
    <key>PlaceName</key>
    <array/>
</dict>
</plist>

I have two arrays which is populated dynamically and i have to store the contents of that array into this plist.How can i achieve this?

Anybody help me please.

Looking for a positive response...Thank you all in advance....

Sankar Chandra Bose
  • 377
  • 1
  • 12
  • 27

2 Answers2

1

Here you go: (replace "data" with name of your plist)

        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
        NSString *documentsDirectory = [paths objectAtIndex:0]; //2
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"]; //3

        NSFileManager *fileManager = [NSFileManager defaultManager];

        if (![fileManager fileExistsAtPath: path]) //4
        {
            NSString *bundle = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; //5

            [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
        }



        NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile: path];

        if ( [data containsObject:example]  ){
            value = [data count];
            NSString *count;

            count = [NSString stringWithFormat:@"%i", value];
        }
        else {

        [data addObject:example];

        [data writeToFile: path atomically:YES];
        value = [data count];
            NSString *count;

        count = [NSString stringWithFormat:@"%i", value];
        [data release];
    }
Prajoth
  • 900
  • 3
  • 12
  • 26
  • Hi Prajoth,Thanks for your response.Can you please explain me what are you doing in the if...else part after this statement NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile: path]; I need to store two arrays into the plist,i am not clear with your code.Please excuse me... – Sankar Chandra Bose Sep 12 '11 at 04:40
  • Hey Sankar, I used this in my particular application where I was checking if the data that I was adding into the plist already existed in the plist, in which case I dont want to add it to the plist. Thats why I used the if then statement. – Prajoth Sep 13 '11 at 02:21
0

If you're looking out for guidelines into reading / writing to plist files from your app, there are certainly a few existing questions / resources which can help you out from start to end.

Check this guide > http://www.icodeblog.com/2009/02/14/loading-data-from-plist-files/

Check out these questions too;

Community
  • 1
  • 1
Madhu
  • 2,429
  • 16
  • 31