繁体   English   中英

如何避免使用子类化和/或委托模式(Swift)为两个视图控制器编写相同的功能

[英]How to avoid writing the same function for two view controllers using subclassing and/or the delegate pattern (Swift)

我有两个具有相同逻辑的视图控制器(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的扩展来解决它,但是事情有些混乱。 然后我听说了委托模式,但是找不到与这种情况有关的任何示例!

编辑:viewDidAppear方法也完全相同。 如果能以某种方式我可以从超类中重写它?

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

首先,这两个类是完全相同的。 这些类中是否还有其他不同的代码? 如果否,那么您不需要2个相同的课程,只需离开1个课程即可。

如果是,则取决于每个类实现的其他代码。 例如,如果您的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 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-为什么要阅读本教程以了解何时使用继承和协议。

请在处理数字时使用switch,以避免出现其他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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM