iphone - iOS, NSMutableDictionary

标签 iphone objective-c ios nsmutabledictionary

我在我的项目中遇到了一个问题,我在头文件中声明了一个 NSMutableDictionary 属性,如下所示:

@property (copy, nonatomic) NSMutableDictionary *DataDict ;

现在在实现文件中,我继续初始化这个字典,因为我要使用它,就像这样:

DataDict = [[NSMutableDictionary alloc]init];

现在,当我这样做时,当我尝试向该词典添加内容时,我会收到此错误消息:

-[__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'

一段时间后,我的项目运行了一千次,我决定取消注释我的初始化行,就像这样

 //DataDict = [[NSMutableDictionary alloc]init];

这就解决了问题。

我的问题是:为什么?

最佳答案

问题在于您定义属性的方式。如果将其更改为:

@property (strong, nonatomic) NSMutableDictionary *DataDict ;

而不是 copy 一切都应该没问题。

发生这种情况是因为您基本上说您想要通过生成的访问器获取对象的副本,而访问器返回的是 NSDictionary(不可变副本)。

您可以在 objective-c properties here 上找到更多信息.

作为旁注:objective-c ivars 通常以小写字母开头(大写名称用于类),因此 dataDict 应该优先于 DataDict

关于iphone - iOS, NSMutableDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12982346/

相关文章:

ios - 如何从连接的 iOS 设备获取电池充电周期?

Objective-C : Is there any callback invoked when sending a message to the object that can't respond?

ios - Sqlite 总是只返回 1 行

iphone - targetContentOffsetForProposedContentOffset :withScrollingVelocity without subclassing UICollectionViewFlowLayout

iphone - 如何使用 phonegap 成为整个原生 ios 应用程序的一部分

iphone - 有没有办法在 iOS 上读取 HDMI 音频?

iphone - iOS、iPhone - 弹出图像查看器

iphone - UITextAttributeTextShadowOffset 已弃用

iphone - NSData dataWithContentsOfFile 仪器 100% 泄漏

ios - 从 UIBarButtonItem 麻烦显示 UIActionSheet