如何通过额外的功能将重复的代码气味外包出去? iOS Swift功能

时间:2019-02-02 10:09:50

标签: ios swift dry

我尝试重构与DRY原理有关的重复工作代码。 (快速版本4)您可以下载工作代码Project here(不大)。问题是我不知道如何才能正确地针对这些函数正确地执行此操作,因为它们使用ViewController类,与外包函数相比,我无法管理它来传递它们并实例化它们。我需要根据dry来查看改进代码的具体实现,因为我在实现部分失败了,我只知道dry原理是干净的,但我不知道如何在真实代码中实现

我所拥有的:

import Foundation
import UIKit

public final class MainCoordinator {

    var navigationController: UINavigationController

    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
        self.navigationController.setNavigationBarHidden(true, animated: false)
    }

func showHome() {
    var vc: HomeViewController
    var i = 0
    for viewController in navigationController.viewControllers {
        if (viewController.isKind(of: HomeViewController.self)) {
            vc = viewController as! HomeViewController
            navigationController.viewControllers.remove(at: i)
            navigationController.pushViewController(vc, animated: true)
            return
        }
        i+=1
    }

    vc = HomeViewController.instantiate()
    vc.coordinator = self
    navigationController.pushViewController(vc, animated: true)
}

func showDetail() {
    var vc: DetailViewController
    var i = 0
    for viewController in navigationController.viewControllers {
        if (viewController.isKind(of: DetailViewController.self)) {
            vc = viewController as! DetailViewController
            navigationController.viewControllers.remove(at: i)
            navigationController.pushViewController(vc, animated: true)
            return
        }
        i+=1
    }

    vc = DetailViewController.instantiate()
    vc.coordinator = self
    navigationController.pushViewController(vc, animated: true)
}

func showMasterDetail() {
    var vc: MasterDetailViewController
    var i = 0
    for viewController in navigationController.viewControllers {
        if (viewController.isKind(of: MasterDetailViewController.self)) {
            vc = viewController as! MasterDetailViewController
            navigationController.viewControllers.remove(at: i)
            navigationController.pushViewController(vc, animated: true)
            return
        }
        i+=1
    }

    vc = MasterDetailViewController.instantiate()
    vc.coordinator = self
    navigationController.pushViewController(vc, animated: true)
}

}

我想要的东西:

这是示例/伪代码,因为它不起作用。您应该了解我的意思。真实的代码看起来如何?

import Foundation
import UIKit

public final class MainCoordinator {

//..

func showHome() {
    present(HomeViewController)
}

func showDetail() {
    present(DetailViewController)
}

func showMasterDetail() {
    present(MasterDetailViewController)
}

func present(myClass: Class){

    var vc: myClass
    var i = 0
    for viewController in navigationController.viewControllers {
        if (viewController.isKind(of: myClass.self)) {
            vc = viewController as! myClass
            navigationController.viewControllers.remove(at: i)
            navigationController.pushViewController(vc, animated: true)
            return
        }
        i+=1
    }

    vc = myClass.instantiate()
    vc.coordinator = self
    navigationController.pushViewController(vc, animated: true)
}

}

1 个答案:

答案 0 :(得分:2)

我将介绍一个Coordinatable协议:

protocol Coordinatable: class {
  var coordinator: MainCoordinator? { get set }
}

class HomeViewController: UIViewController, Coordinatable {
  var coordinator: MainCoordinator?
}

class MainCoordinator {
  func showHome() {
    let viewController = HomeViewController() // Or instantiate any other way
    present(viewController)
  }

  func present<T: UIViewController>(_ viewController: T) where T: Coordinatable {
    // Do whatever you want here
    viewController.coordinator = self
    navigationController.pushViewController(viewController, animated: true)
  }
}