[英]"unrecognized selector sent to instance" exception error
I've read all the "unrecognized selector sent to instance" answers, but they don't seem to apply to my situation.我已经阅读了所有“无法识别的选择器发送到实例”的答案,但它们似乎不适用于我的情况。
I've setting up a NSMutableDictionary like this...我已经设置了一个 NSMutableDictionary 这样的......
NSMutableDictionary *ObjectDynamic = [NSDictionary dictionaryWithObjectsAndKeys:pObject, @"pFObject",tObject, @"tFObject",nil];
and then at some later point in the code I'm trying to add in another object/key, with this...然后在稍后的代码中我试图添加另一个对象/键,用这个...
[ObjectDynamic setObject:mySprite forKey:@"pSObject"];
But I'm getting an exception on that line, with the...但是我在那条线上遇到了一个例外,其中...
-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance -[__NSDictionaryI setObject:forKey:]: 无法识别的选择器发送到实例
Is it not possible to add in another key/value pair like that?不能像这样添加另一个键/值对吗?
Edit编辑
Simple mistake, I just was trying to make a NSDictionary rather than a NSMutableDictionary!一个简单的错误,我只是想制作一个 NSDictionary 而不是 NSMutableDictionary! Thanks for the answers anyway.无论如何,感谢您的回答。
That's because you initialize an immutable NSDictionary that doesn't have a setObject:forKey:
method.那是因为您初始化了一个不具有setObject:forKey:
方法的不可变 NSDictionary。 Initialize a mutable one instead:改为初始化一个可变的:
NSMutableDictionary *ObjectDynamic = [NSMutableDictionary
dictionaryWithObjectsAndKeys:pObject, @"pFObject",tObject, @"tFObject",nil];
Since Xcode 4.4 you can also use the new dictionary literals to initialize immutable dictionaries very easily and then use mutableCopy
.从 Xcode 4.4 开始,您还可以使用新的字典文字非常轻松地初始化不可变字典,然后使用mutableCopy
。
NSMutableDictionary *objectDynamic = [@{@"pFObject" : pObject,
@"tFObject" : tObject} mutableCopy];
Note, that in Objective-C you should start variable names with lower case letters.请注意,在 Objective-C 中,变量名应该以小写字母开头。
In order to be able to change the content of a dictionary, you need to make NSMutableDictionary
, not an immutable NSDictionary
:为了能够更改字典的内容,您需要制作NSMutableDictionary
,而不是不可变的NSDictionary
:
NSMutableDictionary *ObjectDynamic = [NSMutableDictionary // <<== Here
dictionaryWithObjectsAndKeys:pObject, @"pFObject",tObject, @"tFObject",nil
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.