NSMutableArray writeToFile:atomically always returns NO on device but works fine on simulator
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
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..
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.
精彩评论