Hello all, first time poster so please be gentle.
The objective of my program is a navigation-based app, that reads objects from a plist file. Each row is selectable which opens up a detail view (type, date, description, etc). The user has the option to add "events" (via the AddViewController) which are to be written to the plist file and added to the table.
I'm able to read in the file and even write to the file, however when I write to the file it only inserts the last event and overwrites all previous data. Leaving me with only one entry. On top of that the format is somewhat different from the orginal plist format...
The error is obviously in my write code, but I'll also give you the plist format and read code to give better focus.
PLIST:
Code:
<plist version="1.0">
<dict>
<key>Rows</key>
<array>
<dict>
<key>type</key>
<string>Type1</string>
<key>description</key>
<string>This is the description</string>
etc...
</dict>
<dict>
<key>type</key>
<string>Type2</string>
<key>description</key>
<string>This another description</string>
etc...
</dict>
</array>
</dict>
</plist>
This is my file read code from the applicationDidFinishLaunching method:
Code:
//Check to see if file exists
//Resouce directory path
NSString *resourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Data.plist"];
//Documents directory path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DataPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Data.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
//see if Data.plist exists in the Documents directory
if (![fileManager fileExistsAtPath:DataPath]) {
[fileManager copyItemAtPath:resourcePath toPath:DataPath error:nil];
}
//END OF CHECK TO SEE IF FILE EXISTS
//Load Data.plist from documents directory
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:DataPath];
NSDictionary *tempDict = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
self.data = tempDict;
[tempDict release];
self.data is a NSDictionary. Should it be a NSMutableDictionary?!?
Lastly the following is my save method from my AddViewController:
Code:
- (void)save:sender {
//Gets type from UISegmentedControl
NSInteger segIndex = [segType selectedSegmentIndex];
type = [segType titleForSegmentAtIndex:segIndex];
//Gets description from UITextView
description = descriptionView.text;
//Gets date from UIDatePicker
date = [dateAndTime date];
//Writes to plist file
NSString *errorDesc;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *DataPath = [documentsDirectory stringByAppendingPathComponent:@"Data.plist"];
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: type, date, description, nil] forKeys:[NSArray arrayWithObjects:@"type", @"date", @"description", nil]];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];
if (plistData) {
[plistData writeToFile:DataPath atomically:YES];
}
[self.navigationController popViewControllerAnimated:YES];
}
Any help is greatly appreciated.