-(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]];
}
错误:
-[NSCFString setObject:forKey:]: unrecognized selector sent to instance
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString setObject:forKey:]: unrecognized selector sent to instance
我可以从plist中加载"Counter"的值。如果我想要保存相同关键字“Counter”的新值……出现错误。需要帮助,已经花费了几个小时。
再见
这是初始化theDictionary
的代码:
-(void)initDictionary {
if (theDictionary == nil) {
NSString *path=[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
theDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
theString = [theDictionary objectForKey:@"Counter"];
}
}
我找到了!
theString = [[NSString alloc] initWithFormat:[theDictionary objectForKey:@"Counter"]];
感谢大家的支持!