I have an app that I want the user to be able to set some default. They are stored in .plist file.
On app Launch it checks for the file in Documents, and if it doesn't find it, it copies the bundled file.
I can read the values from the plist in the Documents, but if I try and update them in
- (void)applicationDidEnterBackground:(UIApplication *)application
then
NSMutableDictionary* updateVal=[[NSMutableDictionary alloc]initWithContentsOfFile:docPath];
Crashes the app with "EXC_BAD_ACCESS" but only if the values have changed.
Full Code
- (void)applicationDidEnterBackground:(UIApplication *)application{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *docPath = [docDir str开发者_如何学JAVAingByAppendingPathComponent:@"settings.plist"];
NNSMutableDictionary* updateVal=[[NSMutableDictionary alloc]initWithContentsOfFile:docPath];
NSNumber* defWidthSet = defaultWidthDel;
NSNumber* defAspectSet = defaultAspectDel;
[updateVal setObject:defWidthSet forKey:@"defaultWidth"];
[updateVal setObject:defAspectSet forKey:@"defaultAspect"];
[updateVal writeToFile:docPath atomically:YES];
[updateVal release];
I have no idea what I'm doing wrong.
EDIT:
Got it Working...
New Code
- (void)applicationDidEnterBackground:(UIApplication *)application{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"settings.plist"];
NSMutableDictionary* updateVal=[[NSMutableDictionary alloc]initWithContentsOfFile:docPath];
NSNumber* defWidthSet = defaultWidthDel;
NSNumber* defAspectSet = defaultAspectDel;
[updateVal setObject:defWidthSet forKey:@"defaultWidth"];
[updateVal setObject:defAspectSet forKey:@"defaultAspect"];
[updateVal writeToFile:docPath atomically:NO];
[updateVal release];
Can anyone tell me what was wrong about the first way I was doing it?
Got it working
- (void)applicationDidEnterBackground:(UIApplication *)application{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *docPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"settings.plist"];
NSMutableDictionary* updateVal=[[NSMutableDictionary alloc]initWithContentsOfFile:docPath];
NSNumber* defWidthSet = defaultWidthDel;
NSNumber* defAspectSet = defaultAspectDel;
[updateVal setObject:defWidthSet forKey:@"defaultWidth"];
[updateVal setObject:defAspectSet forKey:@"defaultAspect"];
[updateVal writeToFile:docPath atomically:NO];
[updateVal release];
精彩评论