-(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"]];谢谢大家!
发布于 2014-06-27 12:20:43
NSMutableArray *myObjFromFile = ....;
NSMutableDictionary *tmpDictFromFile =
[[[myObjFromFile objectAtIndex:xx] mutableCopy]; mutableCopy];
[tmpDictFromFile setObject:"YOUR OBJECT"
forKey:"YOUR KEY"];https://stackoverflow.com/questions/8438054
复制相似问题