[英]Pass two different types of object as one function variable
I want to create a function that takes two different subclasses of UIViewController. 我想创建一个函数,该函数需要UIViewController的两个不同的子类。 One is a UITableViewController, and one is a UIViewController (both inherit from UIViewController).
一个是UITableViewController,一个是UIViewController(均继承自UIViewController)。 In my function, I want to then check which type the ViewControllers are, in order to access their properties in my function.
在我的函数中,我想检查一下ViewControllers的类型,以便在我的函数中访问它们的属性。 How can I do this, if it is possible?
如果可能,我该怎么办? I have tired the following:
我已经厌倦了以下几点:
internal func myFunction(var controller: UIViewController) {
if controller is MyController {
controller = controller as! MyController
}
// Get errors saying my MyController properties are not available. Type of UIViewController has no member.
}
If I understand you right, you would like to know which type is your parameter. 如果我理解正确,那么您想知道哪种类型是您的参数。 If so, use this kind of method:
如果是这样,请使用这种方法:
internal func myFunction(controller: UIViewController) {
if let menu = controller as? MenuController {
//menu is you MenuCOntroller
} else if let table = controller as? UITableViewController {
//table is your UITableViewController
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.