开发者

Is it possible to declare a chain of super classes in interface declaration?

开发者 https://www.devze.com 2023-01-17 05:37 出处:网络
This may be a silly question but I haven\'t found any information on it. Let\'s say several of the classes in my program derive from \'MySubView\' which is derived from another class, UIViewControlle

This may be a silly question but I haven't found any information on it.

Let's say several of the classes in my program derive from 'MySubView' which is derived from another class, UIViewController.

I would declare it like this:

@interface NewViewController : MySubView {
    // code ...
}
@end

In the future the client wants a change, and desires another view with a table. So I would need to make another class, called MySubTableView, that i开发者_运维知识库s a UITableViewController subclassed from MySubView.

I was thinking this would be easier if I could do something like this:

@interface NewViewController : UITableViewController : MySubView {
    // code ...
}
@end

But this doesn't work.

Is there a way to do this with Xcode, or do I have to specifically make the class itself?

EDIT:

I'm not looking for multiple inheritance. A straight inheritance hierarchy would follow:

NewViewController

UITableviewController

MySubView

UIViewController


No, Objective-C doesn't support declaring those kind of (vertical) inheritance chains. You can only specify the direct super class.

Even if it was possible, there would be problems like calling the correct initializers as they won't be called automatically. Consider a hierarchy like A : B : C - now you can initialize B using e.g. [super init] in As initializer, but how would B know what initializer you want it to call for C?


Objective-C doesn't support multiple inheritance... But Objective-C programmers rarely miss it, because you can accomplish many of the same tasks using Categories instead. Read up on Objective-C Categories.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号