实现自定义 NSMutableArray

发布于 2025-01-02 05:12:18 字数 495 浏览 2 评论 0原文

我想为我的自定义对象创建自己的自定义 NSMutableArray:

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

这个类将存储一个课程数组:

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

我希望“AllCourses”继承自 NSMutableArray,这样我就可以像任何其他 NSMutableArray 对象一样使用它,并从中添加\删除课程并将其加载到 tableView 等中... 所以我的问题是,如果你愿意的话,我的“AllCourses”的实现应该是什么样子?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Spring初心 2025-01-09 05:12:18

首先。如果您想扩展一个类,您需要这样做:

@interface YourCustomClass : SuperClass

通过这种方式,YourCustomClass 继承您的SuperClass 的属性和/或方法。

关于你的问题,苹果文档在 NSMutableArray 类参考

通常没有什么理由子类化 NSMutableArray。班级
很好地完成了它的设计目的——维护一个可变的、有序的
对象的集合。

您可以在此 stackoverflow 主题中找到相同的建议: should-i-subclass-the-nsmutablearray -类

如果您无论如何都想子类化 NSMutableArray,请参阅第一个链接(NSMutableArray 类参考)。您必须重写 5 个方法(请参阅“重写方法”部分)。

在我看来,您可以以传统方式使用 NSMutableArray:创建一个 NSMutableArray 实例并向其添加对象(这里是一个简单的示例)。

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

希望有帮助。

编辑

要覆盖某个方法,您需要在 .m 文件中插入该方法并在其中添加一些逻辑。例如(这里只是伪代码):

//.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 

但是,我再次建议您找到一种不同的方法来实现它,因为(在这种情况下)扩展 NSMutableArray class 并获得一个像原始类一样功能齐全的类。

替代方法是:

  1. 使用类别
  2. 使用组合(在类中使用 NSMutableArray 实例变量)

最后,我还建议您阅读以下讨论:为什么-doesnt-my-nsmutablearray-subclass-subclass-subclass-as-expected。特别需要注意的是

一般来说,您倾向于对系统类进行子类化的频率远低于
您可以使用 C# 或 Java。

正如斯蒂芬·达林顿所说。

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.

酸甜透明夹心 2025-01-09 05:12:18

通常,对这类东西进行子类化并没有真正的用处。您最好使用类别。但是,如果您确实坚持子类化,那么您应该这样做。

首先,你的接口声明是错误的。尝试这种方式:

@interface AllCourses : NSMutableArray
@end

另外,您不需要在可变数组中使用可变数组,您所要做的就是使用 self,例如

[self addObject:<your object here>];

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>];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文