2

I would like to create my own custom NSMutableArray of my custom objects:

    @interface Course : NSObject {
        NSString *className;
        NSString *classGrade;
        NSInteger creditHours;
    }

This class will store an array of courses :

    @interface AllCourses : NSObject : NSMutableArray{
        NSMutableArray *arrClasses;
    }

I would like "AllCourses" to inherit from NSMutableArray so i can use it like any other NSMutableArray Object and add\remove courses from it and load it into a tableView etc... So my question is ,if u would be so kind, is what my implementation of "AllCourses" should look like ?

6
  • 3
    Why do you need to subclass NSMutableArray? It does not look like correct practice. Commented Feb 5, 2012 at 11:56
  • How would correct practice look like ? Commented Feb 5, 2012 at 12:01
  • 1
    Depends on what you would like to achieve. Why won't you have standard NSMutableArray instead? Commented Feb 5, 2012 at 12:05
  • 1
    I am assuming the OP wants to achieve something like type-safety from that collection class. Still @Krizz is right, it is definitely not a good idea to subclass any iOS SDK collection class as those are actually class-clusters. Commented Feb 5, 2012 at 12:09
  • Whats wrong with sub-classing ? isnt that what OOP is all about ? Commented Feb 5, 2012 at 12:18

2 Answers 2

6

First of all. If you want to extend a class you need to do this:

@interface YourCustomClass : SuperClass

In this manner YourCustomClass inherits properties and/or methods of your SuperClass.

About your question, Apple doc says in NSMutableArray Class Reference

There is typically little reason to subclass NSMutableArray. The class does well what it is designed to do—maintain a mutable, ordered collection of objects.

You could find the same suggestion in this stackoverflow topic: should-i-subclass-the-nsmutablearray-class.

If you want to subclass NSMutableArray anyway, see the first link (NSMutableArray Class Reference). You must override 5 methods (see section Methods to Ovveride).

In my opinion you could just use NSMutableArray in the traditional way: create an NSMutableArray instance and add objects to it (here a simple example).

NSMutableArray *myArray = [[NSMutableArray alloc] init]; 
NSNumber *myNumber = [NSNumber numberWithInt:2];
[myArray addObject:myNumber];

Hope it helps.

Edit

To override a method, in your .m file, you need to insert that method and add some logic within it. For example (it's only pseudo code here):

//.h

@interface YourClass: NSMutableArray

@end

//.m

@implementation YourClass

- (void)insertObject:(id)anObject atIndex:(NSUInteger)index
{
   // your logic here
}

// do the same for other ones

@end 

BUT, again, I suggest you to find a different way to do it because it's quite difficult (in this case) to extends a NSMutableArray class and obtain a fully functional class like the original one.

Alternative are:

  1. Use Categories
  2. Use composition (inside your class use a NSMutableArray instance variable)

Finally, I also suggest you to read the following discussion: why-doesnt-my-nsmutablearray-subclass-work-as-expected. In particular, you have to note that

In general, you tend to subclass system classes much less often than you would in C# or Java.

as Stephen Darlington said.

Sign up to request clarification or add additional context in comments.

3 Comments

How would i go about overidin those 5 methods ? i am actually looking for the code...
Thank you Flex for your thorough reply. Guess i will take everbody;s advice just use a local NSMutableArray and not a sub-class.
I love how people say, "use categories", yet there is SO LITTLE information out there on how/why to do that, it makes me want to die.
1

Usually, there's no real use to subclassing that kind of stuff. You would be better off using a category. But, if you really insist on subclassing, here's how you should do this.

First of all, your interface declaration is wrong. Try it this way:

@interface AllCourses : NSMutableArray
@end

Also, you don't need to use a mutable array within your mutable array, all you gotta do is use self, like

[self addObject:<your object here>];

2 Comments

Thanks!, will that cover it ? and what is the full syntax for this overloading method ?
You want to overload addObject? I don't remember the whole syntax, but it's easy enough to find on developer.apple.com :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.