ios - 如何避免使用子类化和/或委托(delegate)模式(Swift)为两个 View Controller 编写相同的函数

标签 ios swift

我有两个具有相同逻辑的 View Controller (A 和 B):

  class A: UIViewController {

   override func viewDidAppear(_ animated: Bool) {

       self.contrast(first: nil, second: self.numberOne)
           timer = Timer.scheduledTimer(timeInterval: 0.7, target: self,   selector: #selector(update), userInfo: nil, repeats: true)


  }

  @objc private func update() {

      if count == 0 {
            self.contrast(first: self.numberOne, second: self.numberTwo)
    } else if count == 1 {
            self.contrast(first: self.numberTwo, second: self.numberThree)

    } else if count == 2 {
            self.contrast(first: self.numberThree, second: self.numberFour)
    } else if count == 3 {
            self.contrast(first: self.numberFour, second: nil)
            self.timer.invalidate()
    }

    self.count += 1
}
}

 class B: UIViewController {


  override func viewDidAppear(_ animated: Bool) {

      self.contrast(first: nil, second: self.numberOne)
         timer = Timer.scheduledTimer(timeInterval: 0.7, target: self,  selector: #selector(update), userInfo: nil, repeats: true)
  }

  @objc private func update() {

  if count == 0 {
        self.contrast(first: self.numberOne, second: self.numberTwo)
} else if count == 1 {
        self.contrast(first: self.numberTwo, second: self.numberThree)

} else if count == 2 {
        self.contrast(first: self.numberThree, second: self.numberFour)
} else if count == 3 {
        self.contrast(first: self.numberFour, second: nil)
        self.timer.invalidate()
}

self.count += 1
      }

    }

如您所见,它们都使用完全相同的代码。我试图使用 UIViewController 的扩展来解决它,但事情变得有点困惑。然后我听说了委托(delegate)模式,但我找不到任何与此场景相关的示例!

编辑:viewDidAppear 方法也完全相同。如果我能以某种方式从父类(super class)中覆盖它?

最佳答案

实际上,有几种方法可以避免重复代码。问题是您应该了解这些类的逻辑以及它们之间的相互关系,以决定如何实现它们。

首先,这两个类是完全一样的。这些类中是否有其他不同的代码?如果不是,则不需要 2 个相同的类(class),只需留下 1 个类(class)即可。

如果是,则取决于每个类实现的其他代码。例如,如果您的 B 类应该与 A 类完全相同并且继承了它的所有特性并且还添加了一些新特性,那么看起来 B 类是 A 类的子类。

classA: UIViewController {
    override func viewDidAppear(_ animated: Bool) {
        self.contrast(first: nil, second: self.numberOne)
        timer = Timer.scheduledTimer(timeInterval: 0.7, target: self,   selector: #selector(update), userInfo: nil, repeats: true)
    }

    @objc private func update() {
        if count == 0 {
            self.contrast(first: self.numberOne, second: self.numberTwo)
        } else if count == 1 {
            self.contrast(first: self.numberTwo, second: self.numberThree)
        } else if count == 2 {
            self.contrast(first: self.numberThree, second: self.numberFour)
        } else if count == 3 {
            self.contrast(first: self.numberFour, second: nil)
            self.timer.invalidate()
        }
        self.count += 1
    }
}

classB: classA {
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        addNewFeatures()
    }

    func addNewfeatures {
        //some additional code
    }
}

如果两个类不同并且彼此不相关(如父子类),您可以改变某些协议(protocol)中的常见行为,通过扩展将默认实现添加到该协议(protocol)并指示您的类将实现该协议(protocol)。

protocol Contrasting {
    func contrast(first:Int, second: Int)
}

extension Contrasting {
    func contrast(first: Int, second: Int) {
        //make contrast
    }
}

classA: UIViewController, Contrasting {
    override func viewDidAppear(_ animated: Bool) {
        contrast(first: nil, second: self.numberOne)
    }
}

classB: UIViewController, Contrasting {
    override func viewDidAppear(_ animated: Bool) {
        contrast(first: nil, second: self.numberOne)
    }
}

https://krakendev.io/blog/subclassing-can-suck-and-heres-why阅读本教程以了解何时使用继承和协议(protocol)。

请在处理数字时使用 switch 以避免大量 else if 语句。

switch count {
    case 0:
        self.contrast(first: self.numberOne, second: self.numberTwo)
    case 1:
        self.contrast(first: self.numberTwo, second: self.numberThree)
    case 2:
        self.contrast(first: self.numberThree, second: self.numberFour)
    case 3:
        self.contrast(first: self.numberFour, second: nil)
    default:
        break
}

关于ios - 如何避免使用子类化和/或委托(delegate)模式(Swift)为两个 View Controller 编写相同的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44467933/

相关文章:

ios - UITableViewCell 中的约束不起作用

arrays - 在 Swift 中将字符串数组添加到 firebase

JSON 解码器 Swift 4.0

ios - Swift - 将参数从非泛型函数传递到泛型函数

ios - 使用 Parse (Swift 3) 注册用户时如何添加头像

ios - Firebase 添加 iOS 应用程序错误 - 处理请求时出现未知错误。再试一次

ios - Swift 手动旋转 View Controller

ios - 如何从 Today View Widget 引用 AppDelegate?

ios - CKError localizedDescription

ios - Swift下载速度说明