Swift - 对具有公共超类的子类的类型约束

时间:2015-03-25 15:02:54

标签: ios swift inheritance protocols type-constraints

一般说明:

我需要能够在我所制作的UIViewController和UITableViewController的2个子类的配置器类实例中使用,但我需要检查它是否是这两个子类中的一个,而不仅仅是一般类,所以在方法定义我期待这种类型的2(它们都符合通用协议)。

深度:

我有一个协议

protocol UIViewControllerNavigationBarConfig
{
    var doneButtonTitle : String? { get set }

    func configureNavigationBar()

    func disableNavigationBarConfig()
}

我有一个UIViewController(ViewControllerWithNavigationBarConfig)的子类,它采用这个协议,也是UITableViewController(TableControllerWithNavBarConfig)的子类,它也采用这个协议。

现在,我有一个配置器类,它知道如何使用这里的近似定义自定义我的控制器(我jus不包括其他函数和func体):

class ControllerConfigurator<NavigationBarConfigurable : UIViewController where NavigationBarConfigurable : UIViewControllerNavigationBarConfig>
{
    class func configureNavigationBarWithInfo(controller: NavigationBarConfigurable)
}

问题是......如果我在类定义(ControllerConfigurator)中定义了类型约束NavigationBarConfigurable,我无法对任何子类使用“configureNavigationBarWithInfo”,因为每次我尝试这样做时,例如: / p>

class SelectQuestionsController: TableControllerWithNavBarConfig
{
    override func viewWillAppear(animated: Bool)
    {
        ControllerConfigurator.configureNavigationBarWithInfo(self)
    }
}

我收到“类型UIViewController不符合协议”,这当然是合乎逻辑的,但我的想法是“我的两个子类都将UIViewController作为超类,所以如果我把条件设置为UIViewController并且符合那个协议,它应该适合我的bot子类“。

如何定义类型约束以使其起作用?

0 个答案:

没有答案