Swift – types和协议的属性

在我的一个Objective-C类中,我有一个types为UIViewController<UIProfileListHeaderDelegate>的属性,我将如何在Swift中表示这个属性? 我需要从UIViewControllerUIProfileListHeaderDelegate访问属性和方法。

谢谢!

处理这个问题的快速方法是定义一个包含UIViewController的相关方法的协议,让你的UIProfileListHeaderDelegateinheritance它。

用你关心的方法定义一个协议:

 protocol ViewControllerSubset { var view:UIView! } 

声明UIViewController实现协议,不需要实际执行任何东西,因为它已经在那里了

 extension UIViewController : ViewControllerSubset {} 

inheritance你的协议

 protocol UIProfileListHeaderDelegate : ViewControllerSubset 

而你走了。

如果你不想改变你的协议的层次结构(或不能,因为它是一个系统协议),你可以定义一个包含两种协议的协议)

 protocol EquatableAndComparable : Equatable, Comparable { }