Objective C - NSArray 子类化
我正在尝试对 NSArray 进行子类化,但在尝试访问 count 方法时它会导致应用程序崩溃。我知道 NSArray
是一个类簇。
- 但这意味着什么?
- 有没有办法可以子类化 NSArray?
我知道我可以简单地子类化 NSObject
并将我的数组作为实例变量,但我宁愿子类化 NSArray
。
编辑: 原因: 我正在创建一个纸牌游戏,我有一个类 Deck
,它应该子类 NSMutableArray
以获得一些额外的方法(-shuffle
,-removeObjects:
、-renew
等),我认为子类 NSArray
看起来比使用 var 更干净。
I am trying to subclass NSArray
, but it crashes the app when trying to access the count method. I know that NSArray
is a class cluster.
- But what does this mean?
- Is there a work around to be able to subclass an NSArray?
I know that I can simply subclass NSObject
and have my array as an instance variable but I would rather subclass NSArray
.
EDIT:
Reason:
I am creating a card game, I have a class Deck
which should subclass NSMutableArray
to have a couple of extra methods (-shuffle
, -removeObjects:
, -renew
, etc), and I think it will look cleaner to subclass NSArray
rather than having a var.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
像这样在类上添加类别的问题是该类的所有实例都将继承附加方法。这既是不必要的(因为并非每个数组都需要能够被洗牌等),也是危险的(因为你无法从类型检查中受益,以确保你当前引用的 NSArray 确实是预期被洗牌的数组) )。
另一种方法是创建您自己的 Deck 类,该类具有 NSMutableArray 作为实例变量。在那里,您可以按照自己的意愿在甲板上定义操作,并且您使用 NSMutableArray 的事实成为实现细节。这使您可以在编译时利用类型检查,并且可以在不更改其客户端的情况下更改 Deck 类的内部实现。例如,如果您出于某种原因决定 NSMutableDictionary 将是更好的后备存储,那么您可以在 Deck 类的实现中进行所有这些更改,而无需更改创建和使用 Deck 的任何代码。
The problem with adding a category on a class like this is that all instances of the class will inherit the additional methods. That's both unnecessary (since not every array needs to be able to be shuffled, etc.) and dangerous (because you can't benefit from typechecking to be sure the NSArray you're currently referring to is really one that was expected to be shuffled).
An alternative would be to create your own Deck class that has an NSMutableArray as an instance variable. There you can define actions on your deck exactly as you would like, and the fact that you are using an NSMutableArray becomes an implementation detail. This lets you take advantage of typechecking at compile-time and it lets you change the internal implementation of your Deck class without changing its clients. For instance, if you decided for some reason that an NSMutableDictionary would be a better backing store, you can make all those changes within the implementation of your Deck class without changing any of the code that creates and uses the Deck.
您通常不需要对其进行子类化,但无论如何,Apple 提出的建议是:
您实际上重写了
count
方法吗?正如他们所说,您必须提供自己的支持结构来保存数组元素,并考虑到这一点覆盖建议的方法。You usually won't need to subclass it, but in any case the suggestions made by Apple are:
Did you actually override
count
method? As they say you have to provide your own backing structure to hold array elements, and override suggested methods considering this..如果您只是添加新方法并使用现有的后备存储,那么更好的方法是向 NSArray 添加类别。类别是 Objective-C 中非常强大的一部分 - 请参阅 cocoadev 了解一些示例。
If you're just adding new methods, and using the existing backing store, then a better approach is to add a category to NSArray. Categories are a really powerful part of objective-C - see cocoadev for some samples.
NSMutableArray
已经有一个- (void)removeObjectsInArray:(NSArray *)otherArray;
你最好创建一个带有可变数组属性的 NSObject 子类。
NSMutableArray
already has a- (void)removeObjectsInArray:(NSArray *)otherArray;
You're going to be best off making an NSObject subclass with a mutable array property.
在这种特殊情况下,我将使用 -
sortedArrayUsingComparator:
对数组进行洗牌,并使比较器随机返回NSOrderedAscending
或NSOrderedDescending
。例如:
In this particular case, I'd shuffle the array using -
sortedArrayUsingComparator:
and make your comparator randomly returnNSOrderedAscending
orNSOrderedDescending
.E.G: