objective c - Subclassing NSArray & NSMutableArray -


the references nsarray , nsmutablearray mention possibility create subclasses, possible providing own backing store , implementation of methods

  • count

  • objectatindex:

for nsarray, as

  • insertobject:atindex:

  • removeobjectatindex:

  • addobject:

  • removelastobject

  • replaceobjectatindex:withobject:

for nsmutablearray. can misleading in leads programmer think not possible simple means subclass nsarray , nsmutablearray.

thought not possible create "simple" subclasses of them make use of existing backing store (even when don't access them directly), still possible little "workaround".

so while looking possibility still able subclass them, had simple idea: create subclasses , use instance of nsarray or nsmutablearray backing store.

here how 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 want subclass nsarray, provide section titled nsarray. can subclass "implementation of custom nsarray subclass" , work wish.

hope helped... peace!

tomen =)

subclassing nsmutablearray , backing nsmutablearray pointless , horrific idea. if you're going subclass basic ns(mutable)array, @ least have reason so. example, have subclasses nsmutablearray , backed c array make act circular buffer, insertion , removal @ front fast @ back. (google chcircularbuffer if you're curious.) however, of time there little or no point in subclassing. also, although may simple create trivial subclass, not trivial create useful , meaningful subclass.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -