Extend a generic class in swift

Patriks

I have some code which has to be available on all UIViewController of application. So I created a class UIViewControllerExtension: UIViewController, which will be extended by each class which I want to use as UIViewController. Which works as expected.

Now I have new screens where I have to use UITableViewController, so I can't extend same class UIViewControllerExtension, And to keep code centralized so I do not want to create another class UITableViewControllerExtension with same code, and want to have a common solution for both cases.

I tried various ways to extend generic class <T:UIViewController> so I can use it in both cases, but it didn't work (as it wouldn't compile). I did some research on internet but didn't find any solution to it. Does someone had same issue and have a solution?

I thought if there would be some solution like

class  CommonViewController<T:UIViewController>: T{    //I know it doesn't compile
//...
}

Usage:

class MyHomeScreenViewController: CommonViewController<UIViewController>{
}

class MyItemListScreenViewController: CommonViewController<UITableController>{
}

I am open to any other solution if it solves my problem.

Edit: More details

1> I would like to extend viewDidLoad() method of UIViewController and UITableViewController in common way (no duplication of code as said before)

2> I would like to add some supporting methods to UIViewController (and UITableViewController), supporting methods like navigateBack, loginUser(name:String,password:String) etc..

Qbyte

A solution would be to extend UIViewController to add additional functionality to all UIViewControllers and override viewDidLoad in your own classes:

extension UIViewController {
    func navigateBack() {
        ...
    }

    // an extension cannot override methods
    // so this method gets called later in an overridden viewDidLoad
    func viewDidLoadNavigate() {
        ...
    }
}

// you own classes
class MyHomeScreenViewController: UIViewController {

    // you have to make sure that all view controllers which can navigate override viewDidLoad
    override viewDidLoad() {
        // optional call to super
        super.viewDidLoad()
        // this is needed and called from the extension
        viewDidLoadNavigate()
    }
}

class MyItemListScreenViewController: UITableViewController {

    override viewDidLoad() {
        super.viewDidLoad()
        viewDidLoadNavigate()
    }
}

As you can see there is some code duplication but this is necessary since UITableViewController can also override viewDidLoad.

If generic inheritance is possible some day this code duplication can be reduced.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

extend Comparable generic in a final class

From Dev

How to extend generic class to be also generic

From Dev

extend a concrete instance of a generic type in Swift

From Dev

extend a concrete instance of a generic type in Swift

From Dev

How to extend from AsyncTask as a generic base class?

From Dev

Is it possible to extend a generic class using extension methods?

From Dev

C# Extend Generic Class in Method call

From Dev

C# Extend Generic Class in Method call

From Dev

Generic class inheritance in Swift

From Dev

Swift delegate for a generic class

From Dev

Swift Generic Class

From Dev

Swift: Declaration in generic class

From Dev

How to extend generic class returning generic value in C#?

From Dev

Extending abstract method with generic parameter (that must extend generic class) in Java

From Dev

Is it possible to extend a generic class while restricting the generic type?

From Dev

Swift: Compare generic Types in generic class

From Dev

Swift: Compare generic Types in generic class

From Dev

Swift use generic function in generic class

From Dev

Set a generic class as a delegate in swift

From Dev

Generic class that conforms to Comparable in Swift

From Dev

Implementing Protocol In Generic class in swift

From Dev

Passing a swift Class to Generic Function

From Dev

Multiple generic class inheritance in Swift

From Dev

Swift generic class, inheritance and covariance

From Dev

How to extend Generic abstract Class with <E extends Comparable<E>>

From Dev

Using a bounded generic class to extend another generic class produces an unknown type

From Dev

Why can't I extend an interface "generic method" and narrow its type to my inherited interface "class generic"?

From Dev

Using a generic Swift class in Objective-C

From Dev

How to implement NSCoding on a generic class in Swift?

Related Related

  1. 1

    extend Comparable generic in a final class

  2. 2

    How to extend generic class to be also generic

  3. 3

    extend a concrete instance of a generic type in Swift

  4. 4

    extend a concrete instance of a generic type in Swift

  5. 5

    How to extend from AsyncTask as a generic base class?

  6. 6

    Is it possible to extend a generic class using extension methods?

  7. 7

    C# Extend Generic Class in Method call

  8. 8

    C# Extend Generic Class in Method call

  9. 9

    Generic class inheritance in Swift

  10. 10

    Swift delegate for a generic class

  11. 11

    Swift Generic Class

  12. 12

    Swift: Declaration in generic class

  13. 13

    How to extend generic class returning generic value in C#?

  14. 14

    Extending abstract method with generic parameter (that must extend generic class) in Java

  15. 15

    Is it possible to extend a generic class while restricting the generic type?

  16. 16

    Swift: Compare generic Types in generic class

  17. 17

    Swift: Compare generic Types in generic class

  18. 18

    Swift use generic function in generic class

  19. 19

    Set a generic class as a delegate in swift

  20. 20

    Generic class that conforms to Comparable in Swift

  21. 21

    Implementing Protocol In Generic class in swift

  22. 22

    Passing a swift Class to Generic Function

  23. 23

    Multiple generic class inheritance in Swift

  24. 24

    Swift generic class, inheritance and covariance

  25. 25

    How to extend Generic abstract Class with <E extends Comparable<E>>

  26. 26

    Using a bounded generic class to extend another generic class produces an unknown type

  27. 27

    Why can't I extend an interface "generic method" and narrow its type to my inherited interface "class generic"?

  28. 28

    Using a generic Swift class in Objective-C

  29. 29

    How to implement NSCoding on a generic class in Swift?

HotTag

Archive