-(void)saveDictionary:(int)counter
{
NSString *path=[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSString *test = [NSString stringWithFormat:@"%d", counter];
[theDictionary setObject:test forKey:@"Counter"]; <---- Error
[theDictionary writeToFile:path atomically:YES];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self saveDictionary:[_viewController counter]];
}
Error:
-[NSCFString setObject:forKey:]: unrecognized selector sent to instance
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString setObject:forKey:]: unrecognized selector sent to instance
I can Load the value for Key "Counter" from plist. If I want to save the new value for same key "Counter" ... Error. Need Help, spent hours.
bye
Here is the Code to initialize theDictionary
:
-(void)initDictionary {
if (theDictionary == nil) {
NSString *path=[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
theDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
theString = [theDictionary objectForKey:@"Counter"];
}
}
Found it!
theString = [[NSString alloc] initWithFormat:[theDictionary objectForKey:@"Counter"]];
Thanks All!
This is because the object stored in theDictionary
is actually an NSString
and NSString
doesn't contain a method called -setObject:forKey
. Check your code for any places where theDictionary
is being assigned and be sure that is actually an NSMutableDictionary
.