[英]iOS, NSMutableDictionary
I had a problem in my project where I declared in the header file an NSMutableDictionary property like so:我在我的项目中遇到了一个问题,我在头文件中声明了一个 NSMutableDictionary 属性,如下所示:
@property (copy, nonatomic) NSMutableDictionary *DataDict ;
Now in the implementation file, I go ahead and initialise this dictionary because I am gonna use it, like so:现在在实现文件中,我继续初始化这个字典,因为我要使用它,就像这样:
DataDict = [[NSMutableDictionary alloc]init];
Now when I did this, the minute I try to add something to this dictionary I would get this error message:现在,当我这样做时,当我尝试向该词典添加内容时,我会收到此错误消息:
-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x885ae60 2012-10-19 16:51:56.040 testing[2297:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x885ae60' -[__NSDictionaryI setObject:forKey:]: 无法识别的选择器发送到实例 0x885ae60 2012-10-19 16:51:56.040 testing[2297:c07] *由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'-[__NSDictionaryI setObject: forKey:]: 无法识别的选择器发送到实例 0x885ae60'
After a while and running through my project a thousand times, I decided to uncomment my initialization line, like so一段时间后,我的项目运行了一千次,我决定取消注释我的初始化行,就像这样
//DataDict = [[NSMutableDictionary alloc]init];
and that fixed the problem.这解决了问题。
My question is: Why?我的问题是:为什么?
The problem is in the way you have defined your property.问题在于您定义属性的方式。 If you change it to:如果将其更改为:
@property (strong, nonatomic) NSMutableDictionary *DataDict ;
instead of copy
everything should be fine.而不是copy
一切都应该没问题。
This happens because you basically say that you want a copy of your object through your generated accessors, which returns an NSDictionary
instead (an immutable copy).发生这种情况是因为您基本上说您想要通过生成的访问器获取对象的副本,而访问器返回的是NSDictionary
(不可变副本)。
You can find more on objective-c properties here .您可以在此处找到有关 objective-c 属性的更多信息。
Just as a sidenote: objective-c ivars usually start with a lowercase letter (uppercase names are used for classes), so dataDict
should be preferred over DataDict
.正如旁注:objective-c ivars 通常以小写字母开头(大写名称用于类),因此dataDict
应该优于DataDict
。
It is because the property have "copy" attribute so NSMutableDictionary instance alloc/init-ed is "copy"'ed using "copy" method, and "copy" method create not NSMutableDictionary but NSDictionary.这是因为该属性具有“复制”属性,所以 NSMutableDictionary 实例分配/初始化是使用“复制”方法“复制”的,而“复制”方法创建的不是 NSMutableDictionary,而是 NSDictionary。 ("mutableCopy" will create NSMutableDictionary). (“mutableCopy”将创建 NSMutableDictionary)。
Probably, you can use "retain" instead of "copy" as attributes.也许,您可以使用“保留”而不是“复制”作为属性。
@property (retain, nonatomic) NSMutableDictionary *DataDict ;
Or, just without "copy"/"retain" but use ARC.(Automatic reference counting).或者,只是不使用“复制”/“保留”,而是使用 ARC。(自动引用计数)。
I have this exact problem.我有这个确切的问题。 No combination of retain/copy/strong/weak, etc do the trick.没有保留/复制/强/弱等的组合可以解决问题。 What does work is to create a temporary Mutable Dictionary, load it up and then set my original equal to it.有效的方法是创建一个临时的可变字典,加载它,然后将我的原始字典设置为等于它。
NSMutableDictionary * tempD = [[NSMutableDictionary alloc] init];
[tempD setObject: epsTapeCut forKey:LWPrintParameterKeyTapeCut];
[tempD setObject: epsCopies forKey:LWPrintParameterKeyCopies];
[tempD setObject: epsHalfCut forKey:LWPrintParameterKeyHalfCut];
[tempD setObject: epsPrintSpeed forKey:LWPrintParameterKeyPrintSpeed];
[tempD setObject: epsDensity forKey:LWPrintParameterKeyTapeWidth];
self.ePSprintSettings = tempD;
This fails:这失败了:
[self.ePSprintSettings setObject: epsTapeCut forKey:LWPrintParameterKeyTapeCut];
Declaration:宣言:
@property (nonatomic, retain) NSMutableDictionary *ePSprintSettings;
(But again, no combination of attributes makes a difference.) (但同样,没有任何属性组合会产生影响。)
Initialization:初始化:
self.ePSprintSettings = (NSMutableDictionary *)[myUserDefaults dictionaryForKey:kEpsPrintSettings];
Thank you for helping me understand.谢谢你帮我理解。
I'm happy enough that this works, but I'd like to understand why.我很高兴这行得通,但我想了解原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.