Swift通用variables

我有一个协议,我想有一个types的UIViewController实现mu协议的variables。 如果我尝试做类似的事情:

var delegate:UIViewController<BouncingMenuDelegate>? 

我收到UIViewController是一个非genericstypes的错误。 那么问题是如何访问Objective-C中用于访问委托的模型? 先谢谢你!

你不能像Swift那样声明variablestypes。

解决你的问题最简单的方法是添加UIViewController方法/属性BouncingMenuDelegate要求:

 @objc protocol BouncingMenuDelegate { // Delegate methods func boudcingMenu(bouncingMenu:BouncingMenu, didSelectItemAtIndex index:Int) // required methods/properties from UIViewController var view:UIView { get } var navigationController:UINavigationController? { get } func presentViewController(viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)?) } class BouncingMenu:UIView { weak var delegate:BouningMenuDelegate? } 

如果类实现一个协议,你不关心它实际上是哪个类。 这应该工作:

 var delegate: BouncingMenuDelegate?