Object C - Subclassification of NSArray

I am trying to subclass NSArray , but when I try to access the count method, it crashes the application. I know that NSArray is a cluster of classes.

  • But what does it mean?
  • Is there any work to subclass NSArray?

I know that I can just subclass NSObject and have my array as an instance variable, but I would prefer a subclass of NSArray .

EDIT: Reason: I am creating a card game, I have a Deck class that must have a subclass of NSMutableArray in order to have some additional methods ( -shuffle , -removeObjects: -renew , etc.), And I think it will look cleaner to a subclass of NSArray , not to var.

+5
source share
5 answers

The problem with adding a category to a class like this is that all instances of the class inherit additional methods. This is both unnecessary (because not every array must be shuffled, etc.) and dangerous (because you cannot use the typechecking check to make sure that the NSArray you are currently referencing is really the one that was expected to be will be shuffled).

An alternative would be to create your own Deck class, which has NSMutableArray as an instance variable. There you can define the actions on your deck exactly as you would like, and the fact that you are using NSMutableArray becomes implementation details. This allows you to use type checking at compile time and allows you to change the internal implementation of your Deck class without changing its clients. For example, if for some reason you decide that NSMutableDictionary will be the best backup storage, you can make all these changes to the implementation of your Deck class without modifying any code that Deck creates and uses.

+16
source

Usually you do not need to subclass it, but in any case, Apple's suggestions:

Any subclass of NSArray must override the primitive instance methods count and objectAtIndex: These methods should work in the backup storage that you provide for collection items. In this repository, you can use a static array, a standard NSArray object, or some other type or data mechanism. You can also override, in part or in full, any other NSArray method for which you want to provide an alternative implementation.

Are you really overriding the count method? As the saying goes, you must provide your own support structure for storing array elements and override the suggested methods, taking this into account.

+6
source

If you just add new methods and use your existing backup storage, then the best approach is to add a category to NSArray. Categories are a really powerful part of objective-C - see cocoadev for some samples.

+3
source

NSMutableArray already has - (void)removeObjectsInArray:(NSArray *)otherArray; You would be better off subclassing NSObject with the modified array property.

0
source

In this particular case, I shuffled the array using - sortedArrayUsingComparator: and making your comparator randomly return NSOrderedAscending or NSOrderedDescending .

eg:

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

Source: https://habr.com/ru/post/906608/


All Articles