开发者

multiple inheritance in objective-c with superclasses already made

开发者 https://www.devze.com 2023-04-08 05:39 出处:网络
Say I want to create my view controller class with 2 superclasses; UIViewController and UIView. is this po开发者_Go百科ssible? or is there another way of getting round this? Multiple inheritance is n

Say I want to create my view controller class with 2 superclasses; UIViewController and UIView.

is this po开发者_Go百科ssible? or is there another way of getting round this?


Multiple inheritance is not supported by Objective-C (hopefully, because multiple inheritance is generally synonym of bad design and also lead to potential complex problems like the diamond problem).

Use protocols instead if you need the concept of "common programming interface" (common API for multiple classes)

Note anyway that making a ViewController inherit UIView is nonsense anyway as this is not the same objects at all and in separate parts of the MVC pattern. (UIViewController being in the "Controller" part, probably holding behavior stuff, whereas UIView is the "View" part and not the Controller part, and only have to manage the display/visible representation of your stuff).


There is obviously some misunderstanding of the MVC pattern (omnipresent in Cocoa framework) and I suggest you take some time to read more about MVC. This may not be straightforward at the beginning if you are new to OOP but this is worth understanding as it would be easier for you once you understand it fully.


No multiple inheritance in Obj C. However you can achieve your goals using Protocols, composition and message forwarding. And might I add, just searching for the above 3 keywords in Apple's dev documentation pages is an excellent way to learn and get started.


One of the tasks of a view controller is to carry enough state so that the view can be released and rebuilt to handle low memory situations. So it is a likely a bad idea.

In general, the iOS framework uses delegates as a neat way to combine the behavior of 2 superclasses: you subclass one (or both) and have an instance of the other class as member variable. Neat and simple and probably a better design anyway.

0

精彩评论

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

关注公众号