Objective C - NSArray 子类化

发布于 2024-10-06 14:35:39 字数 474 浏览 0 评论 0原文

我正在尝试对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终遇你 2024-10-13 14:35:39

像这样在类上添加类别的问题是该类的所有实例都将继承附加方法。这既是不必要的(因为并非每个数组都需要能够被洗牌等),也是危险的(因为你无法从类型检查中受益,以确保你当前引用的 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.

沉溺在你眼里的海 2024-10-13 14:35:39

您通常不需要对其进行子类化,但无论如何,Apple 提出的建议是:

NSArray 的任何子类都必须重写原始实例方法countobjectAtIndex:。这些方法必须对您为集合元素提供的后备存储进行操作。对于此后备存储,您可以使用静态数组、标准 NSArray 对象或某些其他数据类型或机制。您还可以选择部分或全部覆盖您想要提供替代实现的任何其他 NSArray 方法。

您实际上重写了 count 方法吗?正如他们所说,您必须提供自己的支持结构来保存数组元素,并考虑到这一点覆盖建议的方法。

You usually won't need to subclass it, but in any case the suggestions made by Apple are:

Any subclass of NSArray must override the primitive instance methods count and objectAtIndex:. These methods must operate on the backing store that you provide for the elements of the collection. For this backing store you can use a static array, a standard NSArray object, or some other data type or mechanism. You may also choose to override, partially or fully, any other NSArray method for which you want to provide an alternative implementation.

Did you actually override countmethod? As they say you have to provide your own backing structure to hold array elements, and override suggested methods considering this..

缘字诀 2024-10-13 14:35:39

如果您只是添加新方法并使用现有的后备存储,那么更好的方法是向 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.

泪是无色的血 2024-10-13 14:35:39

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.

帥小哥 2024-10-13 14:35:39

在这种特殊情况下,我将使用 -sortedArrayUsingComparator: 对数组进行洗牌,并使比较器随机返回 NSOrderedAscendingNSOrderedDescending

例如:

NSArray *originalArray; // wherever you might get this.
NSArray *shuffledArray = [orginalArray sortedArrayUsingComparator:
    ^(id obj1, id obj2) { 
     return random() %  2 ? NSOrderedAscending : NSOrderedDescending;
     }];

In this particular case, I'd shuffle the array using -sortedArrayUsingComparator: and make your comparator randomly return NSOrderedAscending or NSOrderedDescending.

E.G:

NSArray *originalArray; // wherever you might get this.
NSArray *shuffledArray = [orginalArray sortedArrayUsingComparator:
    ^(id obj1, id obj2) { 
     return random() %  2 ? NSOrderedAscending : NSOrderedDescending;
     }];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文