实现自定义NSMutableArray [英] Implementing Custom NSMutableArray

查看:84
本文介绍了实现自定义NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我希望"AllCourses"继承自NSMutableArray,因此我可以像使用任何其他NSMutableArray对象一样使用它,并从中添加\删除课程并将其加载到tableView等中. 所以我的问题是,如果你那么友好,我对"AllCourses"的实现应该是什么样的?

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 ?

推荐答案

首先.如果要扩展课程,则需要执行以下操作:

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

@interface YourCustomClass : SuperClass

以这种方式YourCustomClass继承了SuperClass的属性和/或方法.

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

关于您的问题,Apple文档在 NSMutableArray类参考

About your question, Apple doc says in NSMutableArray Class Reference

通常几乎没有理由将NSMutableArray子类化.班级 很好地完成了其计划要执行的工作-保持可变的,有序的 对象集合.

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.

您可以在以下stackoverflow主题中找到相同的建议: should-i-subclass- -nsmutablearray-class .

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

如果您仍然想对NSMutableArray进行子类化,请参阅第一个链接(NSMutableArray类参考).您必须覆盖5种方法(请参阅转换方法"一节).

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

我认为您可以按照传统方式使用NSMutableArray:创建一个NSMutableArray实例并向其中添加对象(这里是一个简单的示例).

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

希望有帮助.

修改

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

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 ,再次,我建议您找到一种不同的方法,因为在这种情况下扩展NSMutableArray类和获得像原始版本那样的功能齐全的类非常困难(在这种情况下)一个.

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.

替代方法是:

  1. 使用类别
  2. 使用合成(类中使用NSMutableArray实例变量)
  1. Use Categories
  2. Use composition (inside your class use a NSMutableArray instance variable)

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

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

通常,您对系统类进行子类化的频率通常比 您将使用C#或Java.

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

如斯蒂芬·达林顿所说.

as Stephen Darlington said.

这篇关于实现自定义NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆