Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing NSArray & NSMutableArray [closed]

The references for NSArray and NSMutableArray mention the possibility to create subclasses, but this is only possible by providing your own backing store and implementation of the methods

  • count

  • objectAtIndex:

for NSArray, as well as

  • insertObject:atIndex:

  • removeObjectAtIndex:

  • addObject:

  • removeLastObject

  • replaceObjectAtIndex:withObject:

for NSMutableArray. This can be misleading in that it leads the programmer to think that it is not possible by simple means to subclass NSArray and NSMutableArray.

Thought it is not possible to create "simple" subclasses of them that make use of the existing backing store (even when you don't access them directly), it IS still possible by a little "workaround".

So while i was looking for a possibility to still be able to subclass them, i had a simple idea: Just create subclasses and use an instance of NSArray or NSMutableArray as backing store.

Here is how it works:

CSSMutableArray.h

#import <Foundation/Foundation.h>


@interface CSSMutableArray : NSMutableArray {
    NSMutableArray *_backingStore;
}

@end

CSSMutableArray.m

#import "CSSMutableArray.h"


@implementation CSSMutableArray

- (id) init
{
    self = [super init];
    if (self != nil) {
        _backingStore = [NSMutableArray new];
    }
    return self;
}

- (void) dealloc
{
    [_backingStore release];
    _backingStore = nil;
    [super dealloc];
}

#pragma mark NSArray

-(NSUInteger)count
{
    return [_backingStore count];
}

-(id)objectAtIndex:(NSUInteger)index
{   
    return [_backingStore objectAtIndex:index];
}

#pragma mark NSMutableArray

-(void)insertObject:(id)anObject atIndex:(NSUInteger)index
{
    [_backingStore insertObject:anObject atIndex:index];
}

-(void)removeObjectAtIndex:(NSUInteger)index
{
    [_backingStore removeObjectAtIndex:index];
}

-(void)addObject:(id)anObject
{
    [_backingStore addObject:anObject];
}

-(void)removeLastObject
{
    [_backingStore removeLastObject];
}

-(void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
{   
    [_backingStore replaceObjectAtIndex:index withObject:anObject];
}

@end

If you want to subclass NSArray, you only provide the Section titled NSArray. You can now subclass from your "implementation of a custom NSArray subclass" and work as you wish.

Hope this helped... Peace!

Tomen =)

like image 221
Tomen Avatar asked Sep 12 '26 19:09

Tomen


1 Answers

Subclassing NSMutableArray and backing it with an NSMutableArray is a pointless and horrific idea. If you're going to subclass something as basic as NS(Mutable)Array, at least have a reason to do so. For example, I have subclasses NSMutableArray and backed it with a C array to make it act as a circular buffer, so insertion and removal at the front are just as fast as at the back. (Google CHCircularBuffer if you're curious.) However, most of the time there is little or no point in subclassing. Also, although it may be simple to create a trivial subclass, it is NOT trivial to create a useful and meaningful subclass.

like image 104
Quinn Taylor Avatar answered Sep 15 '26 11:09

Quinn Taylor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!