子类中的空白函数是否符合自定义协议? [英] Is a blank function conventional in subclass that conforms to custom protocol?

查看:29
本文介绍了子类中的空白函数是否符合自定义协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有两个主屏幕,目前都只是 UIViewController 的子类.这两个视图控制器非常相似——它们都实现了我的 UIView 自定义子类,称为 HeaderView,负责显示信息和接收用户输入.就目前而言,这段代码是重复的,因为两个视图控制器的 HeaderView 设置是相同的 - 唯一的区别是当用户确认 HeaderView 中的文本输入时会发生什么.

为了减少重复代码,我创建了一个名为 InputViewController(UIViewController 的子类)的类,它包含两个相同的视图控制器的方面.最终,我希望两个视图控制器子类化 InputViewController 而不是 UIViewController.

class InputViewController: UIViewController, InputProtocol {私人让 headerView = HeaderView()覆盖 func viewDidLoad() {super.viewDidLoad()//布局等设置回调()}内部函数 setupCallbacks() {headerView.onUpdate = { (text: String) in//当用户在 headerView 中确认文本输入时调用self.onHeaderUpdate()}}internal func onHeaderUpdate() {}//空白函数}

setupCallbacks()onHeaderUpdate() 是在 InputViewController 遵循的协议中定义的方法.HeaderView 实现了一个回调闭包,由 headerView.onUpdate...

setupCallbacks() 中处理

InputViewController 遵循的协议:

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

为了说明这一点,我画了一张图;

由于我希望 InputViewController 的子类覆盖 onHeaderUpdate() 方法,所以保留 onHeaderUpdate() 的定义是传统的吗?在 InputViewController 中为空白还是有其他解决方案?

解决方案

InputViewControlleronHeaderUpdate()的定义留空是不是惯例

是的,这被称为抽象方法.给它的代码故意崩溃是很常见的,作为一种说法,我的存在只是为了在子类中被覆盖.">

(我应该进一步说,您正在创建的,执行所有子类必须实现的初始配置的基本视图控制器也是正常的.)

I have two main screens in my app, currently both just subclasses of 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. 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.

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. Eventually, I want the two view controllers to subclass InputViewController instead of 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. The HeaderView implements a callback closure that is handled in setupCallbacks() by headerView.onUpdate...

The protocol that InputViewController conforms to:

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?

解决方案

is it conventional to leave the definition of onHeaderUpdate() in InputViewController blank

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.)

这篇关于子类中的空白函数是否符合自定义协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆