创建一个实现特定协议的对象数组 [英] Create an array of objects that implements a specific protocol

查看:27
本文介绍了创建一个实现特定协议的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL;DR

我正在寻找一个数组类型(var array = [TheTypeImLookingFor]()),比如所有子类化UIViewController的对象实现协议MyProtocol.

I'm looking for an array type (var array = [TheTypeImLookingFor]()) like 'all objects that subclasses UIViewController and implements the protocol MyProtocol.

说明

我正在构建一种带有容器视图和嵌入式子视图(控制器)的向导视图.没问题,只要我只有一种 base 类型的子视图控制器,这就会起作用.

I'm building a kind of wizard view with a container view and embedded child views (controller). No problem, this will work as long, as I have only one base type of child view controllers.

由于屏幕的内容,我现在有一堆 MyTableViewController 类型的视图控制器,它是 UITableViewController 和其他具有常规 UIViewControllers 作为基础.

Due to the content of screens, I have now a bunch of view controllers of type MyTableViewController which is a subclass of UITableViewController and other view controllers that have regular UIViewControllers as base.

所有的视图控制器都有一个共同点.默认数据属性 myData: MyObject.

All of the view controllers have one thing in common. A default data property myData: MyObject.

我创建了一个包含此属性的协议 MyProtocol.

I created a protocol MyProtocol that contains this property.

现在,我必须将所有这些视图控制器组合到一个数组中,以将其用作向导步骤.只要我只需要访问视图控制器方法(数组项是 UIViewController 的类型),我就可以使用 var viewControllers = [UIViewController]() 或者如果我只想访问 myData 属性,我将数组项类型更改为 MyObject.

Now, I have to combine all this view controllers into one array to use it as wizard steps. As long as I only have to access the view controller methods (array items are type of UIViewController) I'm able to use var viewControllers = [UIViewController]() or if I wanna only access the myData property, I change the array item type to MyObject.

但问题是,我必须从 UIViewController 和协议访问方法.

But the problem is, I have to access the methods from the UIViewController and from the protocol.

这就是为什么我要寻找一个数组类型,比如所有对象的子类UIViewController实现了协议MyProtocol.

That's why I'm looking for an array type like 'all objects that subclasses UIViewController and implements the protocol MyProtocol.

我试过了:

  • var viewControllers = [UIViewController: MyProtocol]()//是一个字典
  • `var viewControllers = UIViewController where MyProtocol
  • `var viewControllers = UIViewController.conforms(to: MyProtocol)
  • ...

但没有任何事情按预期工作.

But nothing works as expected.

推荐答案

据我所知,目前没有办法输入一些东西来描述从给定类继承的任何东西并且符合到给定的协议.

As far as I know, there's currently no way to type something so that it describes anything which inherits from a given class and conforms to a given protocol.

一种可能的 hacky 解决方法是创建一个包装器类型,以便在您需要将实例视为 MyProtocol 的情况下为您执行类型转换.

One possible hacky workaround would be to just create a wrapper type in order to perform typecasting for you in the case that you need to treat the instance as a MyProtocol.

struct MyProtocolViewController {

    let base: UIViewController

    init<T : UIViewController>(_ base: T) where T : MyProtocol {
        self.base = base
    }

    func asMyProtocol() -> MyProtocol {
        return base as! MyProtocol
    }
}

现在您可以创建一个 [MyProtocolViewController],并且可以将元素视为 UIViewControllerMyProtocol.

Now you can create a [MyProtocolViewController], and can either treat an element as a UIViewController, or a MyProtocol.

// given that ViewController and AnotherViewController conform to MyProtocol.
let viewControllers = [MyProtocolViewController(ViewController()),
                       MyProtocolViewController(AnotherViewController())]

for viewController in viewControllers {
    print(viewController.asMyProtocol().myData)
    print(viewController.base.prefersStatusBarHidden)
}

这篇关于创建一个实现特定协议的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆