Getting NSArray is something you should avoid. From the documentation:
Remember that NSArray is the public interface of the class cluster and that this entails your subclass. Primitive NSArray methods do not include any designated initializers. This means that you must provide the repository for your subclass and implement the primitive methods that directly act on this repository.
This means that when you initialize the array, you do not get an instance of NSArray . You will get an instance of a completely different class that just has the same interface. That's why the subclassification doesn't work the way you think it works: you have to fully implement the repository yourself. This is why the documentation further says:
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.
And last but not least, in any case, you would have had the wrong initialization. You would need to call super :
- (id)init { self = [super initWithObjects:@"One", @"Two", nil]; if (!self) return nil; return self; }
But, as I just said, it's just not that easy. You will get the same exception again. Therefore, you should simply avoid executing NSArray .
What you can do is add a category to add methods to all instances of NSArray .
source share