nimbus
nimbus

Reputation: 141

Subclassing NSMutableArray

I've been watching CP193 classes on itunes and the common message that comes about is reusability.

I'm creating a program that has a list in the form of an NSMutableArray. I will be accessing this class over and over again.

So I created an NSMutableArray class with a function to setup NSStrings in the array

@interface Geology : NSMutableArray
- (void) setuparray;

In the implementation file I have

[super addobjects:@"object1"];

I would like to initialise this object with objects already inside. I imported the header file inside my ViewController with the following code:

Geology *geology = [[Geology alloc] initWithCapacity:5];
[geology setuparray]; 
NSLog(@"%@", [geology objectAtIndex:1];

However the program does not compile with the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSMutableArray initWithCapacity:]: method only defined for abstract class. Define - Geology initWithCapacity:]!

So from that I need to define the capacity inside my new file. However even though I add [Super initWithCapacity] inside the file it still does not work?

edit:

Geology.h

@interface Geology : NSMutableArray
- (void) setupGeology;

@end

Geology.m

@implementation Geology 
- (void) setupGeology{
    [super initWithCapacity:1];
    [super addObject:@"object1"];
}
@end

In my ViewController I have

#import "Geology.h"

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.   
    Geology *geology = [[Geology alloc] initWithCapacity:5];
    [geology setupGeology];
    NSLog(@"%@", [geology objectAtIndex:1]);
}

Upvotes: 0

Views: 1577

Answers (3)

occulus
occulus

Reputation: 17014

You're subclassing NSArray when it's inappropriate. Subclassing is used for specialising or extending. You're not specialising or extending NSArray here - you're just using it. So just use NSArray, as normal; no need to subclass. So you might have a helper data object that contains an NSArray as a property.

Also, I'd expect a subclass of NSArray to at least have 'Array' in the class name.

Note also that when adding functionality to an existing class in Objective C, class categories are sometimes used rather than subclassing. Categories are particularly useful when you want to add new methods to an existing class without adding any new state (i.e. ivars/properties).

Upvotes: 0

Abizern
Abizern

Reputation: 150605

It looks like all you want to do is to create an custom initialiser for a NSMutableArray so that it is better suited to the way you use it in your application.

You don't have to subclass NSMutableArray to do this.

If all you want to do is add functionality to an existing class and not add extra storage, you can just create a category on NSMutableArray that performs your setup for you.

Upvotes: 0

Ankit Srivastava
Ankit Srivastava

Reputation: 12405

First of all you don't have initWithCapacity declared and defined in your .h and .m since NSMutableArray is an abstract class, so you will need to have to define this and secondly I am not sure if this is the correct way to do it... Whenever you need to add extra functionality to an existing class you can always use categories in objective-c. Subclassing is uusally done when you need to add extra properties to a class along with some functionality.

I believe you should look into categories , because subclassing NSMutableArray is a very rare thing and I have never felt the need to do it.

You can refer to this answer for more clarity.

Upvotes: 1

Related Questions