-(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"]];谢谢大家!
发布于 2011-12-09 05:42:28
这是因为存储在theDictionary中的对象实际上是一个NSString,而NSString不包含名为-setObject:forKey的方法。检查您的代码中是否有分配theDictionary的位置,并确保它实际上是一个NSMutableDictionary。
发布于 2013-12-27 13:15:55
检查初始化..你本可以这样做的。
NSMutableDictionary *dictoForSyncing = [NSMutableArray分配初始化];
发布于 2011-12-09 05:46:31
听起来theDictionary是一个字符串,而不是一个NSMutableDictionary。它是在哪里创建的,在此期间发生了什么?
https://stackoverflow.com/questions/8438054
复制相似问题