I have problems with following Objective C Code:
To read a configuration plist for my App I created load and a save Config methods, but after loading a plist correctly into a (newly allocated) NSMutableDictionary I can't change any of the serialized items because of a Bad Access Error whicht seems to be created by wrong Memory Access. The main point is that I created a new NSMutableDictionary and return it as result of the loadConfig Method. If I debug the code the Dictionary is correctly created and has two item/key pairs in it. Now as soon as I try to change a Value by using setValue (or setObject) I get a Bad Access Error in CFRetain.
What Am I doing wrong?
My Code:
Accessing the loaded Config Dictionary:
OBJCHelpers* objcHelpers = [OBJCHelpers alloc];
NSMutableDictionary* dictSettings = [objcHelpers loadConfig:@"configSettings"]; //Debugged here shows a correct Dictionary in
//dictSettings with following two key/value pairs:
// Key: @"SourceSetting" Value: (int)0
// Key: @"DetectionSetting" Value: (int)0
[dictSettings setObject:(int)srcsetNew forKey:@"SourceSetting"]; //Here the crash happens (btw (int)srcsetNew returns in the debugger (int)2 so thats also not the problem)
[dictSettings setObject:(int)detectionsetNew forKey:@"DetectionSetting"];
Function for loading Config (it really returns the config File as Dictionary, checked in Debugger):
- (NSMutableDictionary*)loadConfig : (NSString*) nstrConfigPlistName {
NSArray* nsarraySystemPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* nsstrDocumentsFolder = [nsarraySystemPaths lastObject];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *nsstrConfigPlistFileName = [nstrConfigPlistName stringByAppendingString:@".plist"];
NSString *nsstrPlistStartConfigFile = [[NSBundle mainBundle] pathForResource:nstrConfigPlistName ofType:@"plist"];
NSString *nstrConfigPlistFile = [nsstrDocumentsFolder stringByAppendingPathComponent:nsstrConfigPlistFileName];
if ((![fileManager fileExistsAtPath:nstrConfigPlistFile]) || cboolForcedConfigOverwrite) {
if (![fileManager fileExistsAtPath:nsstrPlistStartConfigFile]) return [NSMutableDictionary alloc];
else {
NSError* error;
if ([fileManager fileExistsAtPath:nstrConfigPlistFile]) [fileManager removeItemAtPath:nstrConfigPlistFile error:NULL];
if (![fileManager copyItemAtPath:nsstrPlistStartConfigFile toPath:nstrConfigPlistFile error:&error])
NSLog(@"Fehler beim kopieren in den Documents Folder : %@ !", [error localizedDescription]);
}
}
return [[NSMutableDictionary alloc] initWithContentsOfFile:nstrConfigPlistFile];
}
Neither returning a NSMutableDictionary with retain or copying it to a new Dictionary with initWithDictionary:[...] copyItems:true helped.
Please help, thanks in Advance
If srcsetNew
is an int
, you should be doing:
[dictSettings setObject:@(srcsetNew) forKey:@"SourceSetting"];
so that the int
is converted into an NSNumber
instance.