0

I'm creating an app that allows you to log board game plays. I'm storing my logs in a plist and I'm trying to figure out how to append the plist. I think I understand the logic but I'm having trouble putting it into code.

My plist looks like so:

plist

Right now I'm trying something like this but I believe it will simply overwrite the plist file instead of appending

//get path for root directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

NSLog(@"paths: %@",paths);

//get path for documents directory
NSString *documentsPath = [paths objectAtIndex:0];
NSLog(@"documentsPath: %@",documentsPath);

//get the path for logs.plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"logs.plist"];


//create dictionary with values
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:myLogTitle, name, players, myNotes, nil] forKeys:[NSArray arrayWithObjects:@"Log Title",@"Name",@"Players","Notes", nil]];

//write out data
 [plistDict writeToFile:plistPath atomically:YES];

Any help would be really appreciated.

  • http://stackoverflow.com/questions/4779877/how-to-write-in-append-mode-for-text-file – Balu May 13 '13 at 04:16
  • To allow modification in object you have to store your data in mutable Object like `NSMutableArray`, `NSMutableDictionary` etc. instead of `NSArray` or `NSDictionary`. i hope its clear enough – Dipen Panchasara May 13 '13 at 05:20

1 Answers1

0

Try Like This

NSString *path = [[NSBundle mainBundle] pathForResource: @"data" ofType: @"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
NSLog(@"dictbeforeAppending..%@",dict);

//getting Previous Array.....
NSMutableArray *prevArray = [[NSMutableArray alloc]init];
prevArray = [[dict valueForKey:@"Root"] valueForKey:@"Logs"];
NSLog(@"prevArray..%@",prevArray);

// Add new stuff to old stuff.....

NSMutableArray *players =[[NSMutableArray alloc]initWithObjects:@"NewPlayer1",@"Newplayer2",@"Newplayer3", nil];//1
NSDictionary *newObject = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Logtiltle2", @"Krish", players, @"it will be more fun", nil] forKeys:[NSArray arrayWithObjects:@"Log Title",@"Name",@"Players",@"Notes", nil]];//2

NSMutableArray *newArray = [[NSMutableArray alloc]init];
[newArray addObject:newObject];
for (int i=0;i<[prevArray count];i++){
    [newArray addObject:[prevArray objectAtIndex:i]];
}

//set all values for Plist......
NSMutableDictionary *allItemsDict = [[NSMutableDictionary alloc]init];
NSMutableArray *Logs = [[NSMutableArray alloc]initWithArray:newArray];
[allItemsDict setObject:Logs forKey:@"Logs"];

NSMutableDictionary *Root = [[NSMutableDictionary alloc]init];
[Root setObject:allItemsDict forKey:@"Root"];

// Now, write to the plist:
[Root writeToFile:path atomically:YES];

NSDictionary *dictafterAppending = [NSDictionary dictionaryWithContentsOfFile: path];
NSLog(@"dictafterAppending..%@",dictafterAppending);

if you stuck at anywhere then please let me know dude...

iSpark
  • 952
  • 7
  • 18
  • Thanks! This has me headed in the write direction I think. So when I'm reading in data from my plist should I access it from the bundle or should I access this temp dictionary? It seems the later because my view that reads in my logs to a table is only showing the data I already had in the plist and no new data. – Lance Laughlin May 13 '13 at 13:48
  • I'm also get null for prevArray the first time the code is ran. Thanks! – Lance Laughlin May 13 '13 at 14:44
  • if u want in the documentdirectory then just copy your plist to it,use dictafterAppending while reloading the tableview. – iSpark May 14 '13 at 04:36
  • initially at first time if u didnt set any values in the plist,means nothing is there in plist then how it can't be null?,it should be null dear.. – iSpark May 14 '13 at 04:42