首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NSMutableDictionary“无法识别的选择器已发送到实例”

NSMutableDictionary“无法识别的选择器已发送到实例”
EN

Stack Overflow用户
提问于 2011-12-09 05:37:59
回答 6查看 7K关注 0票数 3
代码语言:javascript
复制
    -(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]];
    }

错误:

代码语言:javascript
复制
-[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的代码

代码语言:javascript
复制
-(void)initDictionary {
    if (theDictionary == nil) {
        NSString *path=[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
        theDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
        theString = [theDictionary objectForKey:@"Counter"];
    }
}

找到了!

代码语言:javascript
复制
theString = [[NSString alloc] initWithFormat:[theDictionary objectForKey:@"Counter"]];

谢谢大家!

EN

Stack Overflow用户

发布于 2014-06-27 12:20:43

代码语言:javascript
复制
NSMutableArray *myObjFromFile = ....;
NSMutableDictionary *tmpDictFromFile = 
  [[[myObjFromFile objectAtIndex:xx] mutableCopy]; mutableCopy];

[tmpDictFromFile setObject:"YOUR OBJECT"
                    forKey:"YOUR KEY"];
票数 0
EN
查看全部 6 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8438054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档