Ios 具有公共超类的子类的Swift类型约束
一般说明: 我需要能够在配置器类中使用我制作的UIViewController和UITableViewController的2个子类的实例,但我需要检查它是否是这2个子类之一,而不仅仅是一个普通类,因此在方法定义中,我希望这2个子类的类型都符合通用协议 深入: 我有一个协议Ios 具有公共超类的子类的Swift类型约束,ios,swift,inheritance,protocols,type-constraints,Ios,Swift,Inheritance,Protocols,Type Constraints,一般说明: 我需要能够在配置器类中使用我制作的UIViewController和UITableViewController的2个子类的实例,但我需要检查它是否是这2个子类之一,而不仅仅是一个普通类,因此在方法定义中,我希望这2个子类的类型都符合通用协议 深入: 我有一个协议 protocol UIViewControllerNavigationBarConfig { var doneButtonTitle : String? { get set } func configure
protocol UIViewControllerNavigationBarConfig
{
var doneButtonTitle : String? { get set }
func configureNavigationBar()
func disableNavigationBarConfig()
}
我有一个子类UIViewController ViewControllerWithNavigationBarConfig,它采用了这个协议,还有一个子类UITableViewControllerTableControllerWithNavBarConfig,它也采用了这个协议
现在,我有一个configurator类,它知道如何使用近似定义自定义我的控制器,就像这里我没有包括其他函数和func主体:
class ControllerConfigurator<NavigationBarConfigurable : UIViewController where NavigationBarConfigurable : UIViewControllerNavigationBarConfig>
{
class func configureNavigationBarWithInfo(controller: NavigationBarConfigurable)
}
我收到的UIViewController类型不符合协议,这当然是合乎逻辑的,但我的想法是我的两个子类都将UIViewController作为超类,因此如果我将条件设置为UIViewController并符合该协议,它应该适合我的bot子类
如何定义类型约束才能使其工作?我相信,您必须键入对ControllerConfiguration的调用,例如ControllerConfiguration。ConfigureNavigationBarWithInfoSelf根据您的建议,我修改了方法类func configureNavigationBarWithInfocontroller:NavigationBarConfigurable,之后我开始收到许多其他错误,例如NavigationBarConfigurable没有名为doneButtonTitle的成员,即使在协议中指定了此属性没有说要修改声明的方法。我说修改你调用方法的方式。
class SelectQuestionsController: TableControllerWithNavBarConfig
{
override func viewWillAppear(animated: Bool)
{
ControllerConfigurator.configureNavigationBarWithInfo(self)
}
}