实现自定义 NSMutableArray
我想为我的自定义对象创建自己的自定义 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先。如果您想扩展一个类,您需要这样做:
通过这种方式,
YourCustomClass
继承您的SuperClass
的属性和/或方法。关于你的问题,苹果文档在 NSMutableArray 类参考
您可以在此 stackoverflow 主题中找到相同的建议: should-i-subclass-the-nsmutablearray -类。
如果您无论如何都想子类化 NSMutableArray,请参阅第一个链接(NSMutableArray 类参考)。您必须重写 5 个方法(请参阅“重写方法”部分)。
在我看来,您可以以传统方式使用 NSMutableArray:创建一个 NSMutableArray 实例并向其添加对象(这里是一个简单的示例)。
希望有帮助。
编辑
要覆盖某个方法,您需要在 .m 文件中插入该方法并在其中添加一些逻辑。例如(这里只是伪代码):
但是,我再次建议您找到一种不同的方法来实现它,因为(在这种情况下)扩展
NSMutableArray class 并获得一个像原始类一样功能齐全的类。
替代方法是:
NSMutableArray
实例变量)最后,我还建议您阅读以下讨论:为什么-doesnt-my-nsmutablearray-subclass-subclass-subclass-as-expected。特别需要注意的是
正如斯蒂芬·达林顿所说。
First of all. If you want to extend a class you need to do this:
In this manner
YourCustomClass
inherits properties and/or methods of yourSuperClass
.About your question, Apple doc says in NSMutableArray Class Reference
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 anNSMutableArray
instance and add objects to it (here a simple example).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):
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:
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
as Stephen Darlington said.
通常,对这类东西进行子类化并没有真正的用处。您最好使用类别。但是,如果您确实坚持子类化,那么您应该这样做。
首先,你的接口声明是错误的。尝试这种方式:
另外,您不需要在可变数组中使用可变数组,您所要做的就是使用 self,例如
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:
Also, you don't need to use a mutable array within your mutable array, all you gotta do is use self, like