Notifying view controller of changes to any of five types of models I have a swift 3 project with a code that's look like very generic, but I don't know if I can reduce a method like this one :

var pages: [Page] // List of page object

func RefreshList(){
        guard let currentIndex = currentIndex else { return }

        if let listTableViewController: listTableViewController&lt;ModelA&gt; = pages[currentIndex].viewController as? listTableViewController&lt;ModelA&gt; {
            listTableViewController.didRefreshNodesList()
        }
        else if let listTableViewController: listTableViewController&lt;ModelB&gt; = pages[currentIndex].viewController as? listTableViewController&lt;ModelB&gt; {
            listTableViewController.didRefreshNodesList()
        }
        else if let listTableViewController: listTableViewController&lt;ModelC&gt; = pages[currentIndex].viewController as? listTableViewController&lt;ModelC&gt; {
            listTableViewController.didRefreshNodesList()
        }
        else if let listTableViewController: listTableViewController&lt;ModelD&gt; = pages[currentIndex].viewController as? listTableViewController&lt;ModelD&gt; {
            listTableViewController.didRefreshNodesList()
        }
        else if let listTableViewController: listTableViewController&lt;ModelE&gt; = pages[currentIndex].viewController as? listTableViewController&lt;ModelE&gt; {
            listTableViewController.didRefreshNodesList()
        }
    }


The only things that change is the Model(LETTER) is there a way to reduce that ?
The type annotation in

if let listTableViewController: listTableViewController&lt;ModelA&gt; = pages[currentIndex].viewController as? listTableViewController&lt;ModelA&gt;


is not needed because the type is automatically inferred from the
expression on the right-hand side:

if let listTableViewController = pages[currentIndex].viewController as? listTableViewController&lt;ModelA&gt;




According to the Swift API Design Guidelines


  Names of types and protocols are UpperCamelCase. Everything else is lowerCamelCase.


Therefore it should be func refreshList() and ListTableViewController.



To simplify your code, define a protocol for the common methods:

protocol RefreshController {
    func didRefreshNodesList()
    // ...
}


and make ListTableViewController conform to that protocol.
Then you can reduce it to a single test

if let refreshController = pages[currentIndex].viewController as? RefreshController {
    refreshController.didRefreshNodesList()
}