0

I understand I can for instance write a value to a .plist file as such

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"stored" ofType:@"plist"];
NSString *comment = @"this is a comment"; 
[comment writeToFile:filePath atomically:YES];

But If i had for say an Array inside my .plist (gameArray) and I'd like to white comment into a particular index of my array i.e. gameArray[4] ; how would I do this ?

allow me to clarify

  • I have a plist: stored.plist
  • inside my plist there is an array gameArray
  • i would like to update specific indexes of gameArray inside the plist is this possible ?
Taskinul Haque
  • 724
  • 2
  • 16
  • 36

2 Answers2

0

Assuming the contents of 'stored.plist' is an array, you need to instantiate a mutable array from the path:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"stored" ofType:@"plist"];
NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:filePath];
NSString *comment = @"this is a comment"; 

// inserting a new object:
[array insertObject:comment atIndex:4];

// replacing an existing object:
// classic obj-c syntax
[array replaceObjectAtIndex:4 withObject:4];        
// obj-c literal syntax:
array[4] = comment;

// Cannot save to plist inside your document bundle.
// Save a copy inside ~/Library/Application Support

NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] objectAtIndex:0];
NSURL *arrayURL = [documentsURL URLByAppendingPathComponent:[filePath lastPathComponent]];
[array writeToURL:arrayURL atomically:NO];
mrwalker
  • 1,883
  • 21
  • 23
  • yes but now u just have a data type `array` with the `comment` at index 4, this does not write to the .plist, i want to write to the actual .plist – Taskinul Haque Oct 09 '12 at 10:22
  • Added sample code to save to ~/Library/Application Support/stored.plist – mrwalker Oct 09 '12 at 10:50
  • yes but this adds an array to the .plist, it doesn't update the existing one: is it not possible too update the existing one ? – Taskinul Haque Oct 09 '12 at 11:08
  • On first run, you could copy the default plist from your document bundle to ~/Library/Application Support, then update & save to that location. – mrwalker Oct 09 '12 at 12:50
0

You cannot update and save data in application's main bundle instead you have to do in document directory or other directory like this:

 NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistFilePath = [documentsDirectory stringByAppendingPathComponent:@"stored.plist"];

if([[NSFileManager defaultManager] fileExistsAtPAth:plistFilePath]) 
{//already exits

   NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistFilePath];
   //update your array here
   NSString *comment = @"this is a comment";
   [data replaceObjectAtIndex:4 withObject:comment];

   //write file here
   [data writeToFile:plistFilePath atomically:YES];
}
else{ //firstly take content from plist and then write file document directory 

 NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"stored" ofType:@"plist"];
 NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistPath];
 //update your array here
   NSString *comment = @"this is a comment";
   [data replaceObjectAtIndex:4 withObject:comment];

   //write file here
   [data writeToFile:plistFilePath atomically:YES];
}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • thank you for this, but what your code is doing is adding an array `data` into my plist, I have an array in it already, which I'm trying to update . . . . – Taskinul Haque Oct 09 '12 at 10:52
  • Firstly find your root structure say its array or dictinary like this: NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistPath]; or NSMutableDictionary *data = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath]; – Paresh Navadiya Oct 09 '12 at 11:01
  • your structure will be dictionary with arrays – Paresh Navadiya Oct 09 '12 at 11:03