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 ?

Sagi
  • 981
  • 11
  • 15
  • 3
    Why do you need to subclass `NSMutableArray`? It does not look like correct practice. – Krizz Feb 05 '12 at 11:56
  • How would correct practice look like ? – Sagi Feb 05 '12 at 12:01
  • 1
    Depends on what you would like to achieve. Why won't you have standard `NSMutableArray` instead? – Krizz Feb 05 '12 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. – Till Feb 05 '12 at 12:09
  • Whats wrong with sub-classing ? isnt that what OOP is all about ? – Sagi Feb 05 '12 at 12:18
  • OOP is about subclassing, but not *all about*. –  Feb 05 '12 at 12:34

2 Answers2

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.

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • How would i go about overidin those 5 methods ? i am actually looking for the code... – Sagi Feb 05 '12 at 12:29
  • Thank you Flex for your thorough reply. Guess i will take everbody;s advice just use a local NSMutableArray and not a sub-class. – Sagi Feb 05 '12 at 14:36
  • 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. – Alex Gray Feb 12 '12 at 10:11
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>];
Simon Germain
  • 6,834
  • 1
  • 27
  • 42
  • Thanks!, will that cover it ? and what is the full syntax for this overloading method ? – Sagi Feb 05 '12 at 11:59
  • You want to overload addObject? I don't remember the whole syntax, but it's easy enough to find on developer.apple.com :) – Simon Germain Feb 05 '12 at 12:02