Implementing Custom NSMutableArray

落花浮王杯 提交于 2019-12-22 18:38:09

问题


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 ?


回答1:


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.




回答2:


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>];


来源:https://stackoverflow.com/questions/9149000/implementing-custom-nsmutablearray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!