Sherwin Sherwin - 1 year ago 65
Swift Question

How to do multi-inheritance in swift?

I need to implement a class(for convenience name it A) derived from UITableViewController and another(B) from UICollectionViewController. And there are a lot of things common, so I want to put them in a class(C) and let A and B inherit C. Now A and B both have two class to inherit, but multiple inheritance is not allowed in swift, so how to implement this? I know there is no multi-inheritance allowed in swift, but I still want to know how to do the things I described above.

Answer Source

As stated in the comments by @Paulw11 is correct. Here is an example that involves A & B inheriting from C. Which I have named DogViewController and CatViewController (which inherits form PetViewController). You can see how a protocol might be useful. This is just an ultra basic example.

protocol Motion {
    func move()

extension Motion where Self: PetViewController {

    func move() {
        //Open mouth


class PetViewController: UIViewController, Motion{
    var isLoud: Bool?

    func speak(){
        //Open Mouth

class DogViewController:PetViewController {

    func bark() {


        //Make Bark Sound

class CatViewController: PetViewController {

    func meow() {


        //Make Meow Sound




let cat = CatViewController()
cat.isLoud = false