简体   繁体   English

子类中的空白函数是否符合自定义协议?

[英]Is a blank function conventional in subclass that conforms to custom protocol?

I have two main screens in my app, currently both just subclasses of UIViewController .我的应用程序中有两个主屏幕,目前都只是UIViewController子类。 These two view controllers are very similar - they both implement my custom subclass of UIView called HeaderView that is responsible for displaying information and taking user input.这两个视图控制器非常相似——它们都实现了我的UIView自定义子类,称为HeaderView ,负责显示信息和获取用户输入。 As it stands, this code is repetitive because the HeaderView setup is the same for both view controllers - the only difference is what happens when the user confirms the text entry in HeaderView .就目前而言,这段代码是重复的,因为两个视图控制器的HeaderView设置是相同的——唯一的区别是当用户确认HeaderView的文本输入时会发生什么。

To cut down on repetitive code, I am creating a class called InputViewController (a subclass of UIViewController ) that houses the aspects of the two view controllers that are identical.为了减少重复代码,我创建了一个名为InputViewControllerUIViewController的子类)的类,它包含两个相同的视图控制器的方面。 Eventually, I want the two view controllers to subclass InputViewController instead of UIViewController .最终,我希望两个视图控制器InputViewController而不是UIViewController

class InputViewController: UIViewController, InputProtocol {

    private let headerView = HeaderView()
        
    override func viewDidLoad() {
        super.viewDidLoad()
        // layout, etc.
        setupCallbacks()
    }
    
    internal func setupCallbacks() {
        headerView.onUpdate = { (text: String) in
            // called when user confirms text entry in headerView
            self.onHeaderUpdate()
        }
    }
    
    internal func onHeaderUpdate() {} // Blank function
    
}

setupCallbacks() and onHeaderUpdate() are methods defined in the protocol that the InputViewController conforms to. setupCallbacks()onHeaderUpdate()InputViewController遵循的协议中定义的方法。 The HeaderView implements a callback closure that is handled in setupCallbacks() by headerView.onUpdate... HeaderView实现了一个回调闭包,由headerView.onUpdate...setupCallbacks()处理headerView.onUpdate...

The protocol that InputViewController conforms to: InputViewController遵循的协议:

protocol InputProtocol {
    func setupCallbacks()
    func onHeaderUpdate()
}

To illustrate this, I drew up a diagram;为了说明这一点,我画了一张图; 图表

Since I want the subclasses of InputViewController to override the onHeaderUpdate() method, is it conventional to leave the definition of onHeaderUpdate() in InputViewController blank or is there another solution to this?因为我想要的子类InputViewController覆盖onHeaderUpdate()方法,是传统留下的定义onHeaderUpdate()InputViewController空白或有另一种解决方案呢?

is it conventional to leave the definition of onHeaderUpdate() in InputViewController blankonHeaderUpdate()的定义留空是InputViewController

Yes, that is called an abstract method.是的,这叫做抽象方法。 It is common to give it code that crashes deliberately, as a way of saying, “I exist only to be overridden in a subclass.”给它提供故意崩溃的代码是很常见的,作为一种说法,“我的存在只是为了在子类中被覆盖。”

(I should go further and say that what you are creating, a base view controller that carries out initial configurations that all subclasses must implement, is also normal.) (我应该更进一步说,你正在创建的,一个执行所有子类必须实现的初始配置的基本视图控制器也是正常的。)

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

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