Swift通用变量 [英] Swift generic variable
本文介绍了Swift通用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个协议,我想有一个实现mu协议的UIViewController类型的变量。如果我尝试这样做:
I have a protocol, and I'd like to have a variable of type UIViewController implementing mu protocol. If I try to do something like:
var delegate:UIViewController<BouncingMenuDelegate>?
我收到UIViewController是非泛型类型的错误。那么问题是如何访问Objective-C中用于访问代表的模型?谢谢!
I receive error that UIViewController is a non-generic type. Then the question is being how to access the model we used in Objective-C for accessing delegates? Thank you in advance!
推荐答案
您不能在Swift中声明变量类型。
You cannot declare variable type like that in Swift.
解决问题的最简单方法是将 UIViewController
方法/属性添加到 BouncingMenuDelegate
要求: p>
The easiest way to solve your problem is adding UIViewController
methods/properties to BouncingMenuDelegate
requirement:
@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?
}
这篇关于Swift通用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文