Hey guys
So I am working on a app that has a MutableArray in a static class, when the user does an action it saves on to a tableview which is then written into the getters and setters of the mutableArray.
I followed a Tutorial on developer.apple.com for this and this is what i've got
I put [self init] in the method that writes to the array. Now every time click the history tab in the app it crashes and it doesn't write to the plist.... how can I fix this code?
So I am working on a app that has a MutableArray in a static class, when the user does an action it saves on to a tableview which is then written into the getters and setters of the mutableArray.
I followed a Tutorial on developer.apple.com for this and this is what i've got
Code:
- (id) init {
self = [super init];
if (self) {
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString *plistPath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
}
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
if (!temp) {
NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}
self.parkingHistory = [StaticClass get_logObjectsArray:[temp objectForKey:@"history"]];
}
return self;
}
- (void)applicationWillResignActive:(UIApplication *)application {
NSString *error;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: parkingHistory, nil]
forKeys:[NSArray arrayWithObjects: @"history", nil]];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
if(plistData) {
[plistData writeToFile:plistPath atomically:YES];
}
else {
NSLog(@"%@", error);
[error release];
}
}
I put [self init] in the method that writes to the array. Now every time click the history tab in the app it crashes and it doesn't write to the plist.... how can I fix this code?