如何继承NSDictionary? [英] How to inherit from NSDictionary?

查看:83
本文介绍了如何继承NSDictionary?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为设置"的对象,该对象继承自NSMutableDictionary.当我尝试使用

I have an object called Settings that inherits from NSMutableDictionary. When I try to initialize this object using

Settings *settings = [[Settings alloc] initWithContentsOfFile: @"someFile"]

它返回类型为NSCFDictionary的对象.结果,它无法识别我的其他方法.例如,当我将选择器称为保存"时,它会对象:

it returns an object of type NSCFDictionary. As a result, it doesn't recognize my additional methods. For example, when I call the selector "save", it objects:

[NSCFDictionary save]: unrecognized selector sent to instance 0x524bc0

当然,当我使用花园品种进行初始化时就可以了

Of course, it's OK when I initialize using the garden variety

Settings *settings = [[Settings alloc] init]

我试图再次将其投射到设置",但这没有用.这似乎很简单-我想念什么?

I tried to cast it again to Settings but that didn't work. This seems really simple - what am I missing?

谢谢

推荐答案

NSDictionary是一个类集群.这意味着从其init方法返回的值严格来说不是NSDictionary,而是实现实际功能的子类.在几乎每种情况下,最好给您的类一个NSDictionary作为实例变量,或者只在NSDictionary上定义一个类别.

NSDictionary is a class cluster. This means that the value returned from its init methods is not strictly an NSDictionary, but a subclass that implements the actual functionality. In almost every case, it is better to give your class an NSDictionary as an instance variable or to simply define a category on NSDictionary.

这篇关于如何继承NSDictionary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆