1

I have a plist file with root of type Array in the resources in the xcode project. On a button click i need to access this plist and find if the plist already contains the particular item if not write the new item(NSString) to the plist.

I am doing this as follows,

NSMutableArray *array = nil;
NSDictionary *dictionary = nil;
  path = [[NSBundle mainBundle] pathForResource:@"MyPlistName" ofType:@"plist"];
array = [[NSMutableArray alloc] initWithContentsOfFile:path];
dictionary = [self.dictionary objectForKey:@"tag"];

if(![array containsObject:dictionary])
{
    [array addObject:dictionary];
    if(![array writeToFile:path atomically:YES])
    {
        NSLog(@".plist writing was unsucessfull");
    }
     }

This works for me on the simulator but when i run it on the device the writeToFile:atomically method seems to return NO always.

i even tried with NSArchiver even that seems to return me NO as the return value

Can any one tell what am i doing wrong..

Regards,

Syed Yusuf

luvieere
  • 37,065
  • 18
  • 127
  • 179
user162612
  • 61
  • 1
  • 5

2 Answers2

6

You can not write into the application's bundle so you can not write to a plist in the Resources directory. One good way is to copy the plist from the Resource directory into the Documents directory on first launch and access it from there in the future..

zaph
  • 111,848
  • 21
  • 189
  • 228
1

I created this question exactly so you could understand how to get a writable path:

How can I get a writable path on the iPhone?

Copy your document to a writable path and change it there.

Community
  • 1
  • 1
Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150