简体   繁体   English

将两种不同类型的对象作为一个函数变量传递

[英]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.

相关问题 RestKit:两个单独的提要,两种不同的对象类型。 一个对象管理器? - RestKit: two separate feeds, two different object types. One object manager? 将两个不同类型的数组排序为一个 - Sort Two Arrays of Different Types into One Swift 函数返回两种不同的类型 - Swift function returning two different types 在一个标签字符串中显示两种不同类型的变量 - Displaying two different types of variables in one label string 如何在UIPickerView中为两个不同的组件显示两个不同的对象类型? - How do I show two different object types in UIPickerView for two different components? 处理不同类型的对象 - Handle object of different types 一个变量,一个类和两个相同功能的函数迅速 - One variable, one class and two of the same function swift 最好允许对象类型为'id'或为方法提供两种不同类型的参数? - Better to allow an object type of 'id' or provide two arguments for two different types for a method? 如何用来自两个不同对象类型的两个数组的数据填充UITableView? - How can you fill a UITableView with data from two arrays of two different object types? 您的第二个iOS应用程序:在两个不同的视图中编辑一个对象 - Your Second iOS App: Edit one object in two different views
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM