在Swift中,如何声明符合一个或多个协议的特定类型的变量? [英] In Swift, how can I declare a variable of a specific type that conforms to one or more protocols?

查看:1495
本文介绍了在Swift中,如何声明符合一个或多个协议的特定类型的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift中,我可以通过如下声明来明确设置变量的类型:

In Swift I can explicitly set the type of a variable by declaring it as follows:

var object: TYPE_NAME

如果我们想更进一步并声明一个符合多种协议的变量,我们可以使用 protocol 声明性:

If we want to take it a step further and declare a variable that conforms to multiple protocols we can use the protocol declarative:

var object: protocol<ProtocolOne,ProtocolTwo>//etc

如果我想声明一个符合一个或多个协议的对象并且也是特定的基类类型? Objective-C等价物如下所示:

What if I would like to declare an object that conforms to one or more protocols and is also of a specific base class type? The Objective-C equivalent would look like this:

NSSomething<ABCProtocolOne,ABCProtocolTwo> * object = ...;

在Swift我希望它看起来像这样:

In Swift I would expect it to look like this:

var object: TYPE_NAME,ProtocolOne//etc



<这使我们能够灵活地处理基类型的实现以及协议中定义的添加接口。

This gives us the flexibility of being able to deal with the implementation of the base type as well as the added interface defined in the protocol.

是否有另一个更明显的我可能会失踪的方式?

Is there another more obvious way that I might be missing?

举个例子,假设我有 UITableViewCell 工厂负责返回符合协议的单元格。我们可以轻松设置返回符合协议的单元格的泛型函数:

As an example, say I have a UITableViewCell factory that is responsible for returning cells conforming to a protocol. We can easily setup a generic function that returns cells conforming to a protocol:

class CellFactory {
    class func createCellForItem<T: UITableViewCell where T:MyProtocol >(item: SpecialItem,tableView: UITableView) -> T {
        //etc
    }
}

稍后我想在利用类型和协议的同时出列这些单元格

later on I want to dequeue these cells whilst leveraging both the type and the protocol

var cell: MyProtocol = CellFactory.createCellForItem(somethingAtIndexPath) as UITableViewCell

这会返回错误,因为表格视图单元格不符合协议...

This returns an error because a table view cell does not conform to the protocol...

我希望能够指定该单元格是 UITableViewCell 并符合 MyProtocol 在变量声明中?

I would like to be able to specify that cell is a UITableViewCell and conforms to the MyProtocol in the variable declaration?

如果您熟悉< a href =http://en.wikipedia.org/wiki/Factory_method_pattern>工厂模式这在能够返回实现特定接口的特定类的对象的上下文中是有意义的。

If you are familiar with the Factory Pattern this would make sense in the context of being able to return objects of a particular class that implement a certain interface.

就像在我的例子中一样,有时我们喜欢定义应用于特定对象时有意义的接口。我的表格视图单元格的例子是一个这样的理由。

Just like in my example, sometimes we like to define interfaces that make sense when applied to a particular object. My example of the table view cell is one such justification.

虽然提供的类型并不完全符合上面提到的接口,但是工厂返回的对象也是如此喜欢与基类类型和声明的协议接口交互的灵活性

Whilst the supplied type does not exactly conform to the mentioned interface, the object the factory returns does and so I would like the flexibility in interacting with both the base class type and the declared protocol interface

推荐答案

在Swift 4中,现在可以声明一个变量,它是一个类型的子类,同时实现一个或多个协议。

In Swift 4 it is now possible to declare a variable that is a subclass of a type and implements one or more protocols at the same time.

var myVariable: MyClass & MyProtocol & MySecondProtocol

或作为方法的参数:

func shakeEm(controls: [UIControl & Shakeable]) {}

Apple在2017年WWDC上通过会议402:什么宣布了这一消息新的Swift

Apple announced this at WWDC 2017 in Session 402: Whats new in Swift


其次,我想谈谈编写类和协议。所以,这里
我已经为一个UI元素引入了这个可动摇协议,它可以给
带来一点震动效果来引起对自身的注意。而且我已经花了
并扩展了一些UIKit类来实际提供这个摇动
功能。现在我想写一些看似简单的东西。 I
只是想编写一个函数,它需要一堆
shakable的控件,然后动摇那些能够引起人们注意
的控件。我可以在这个数组中写什么类型的?这实际上是
令人沮丧和棘手。所以,我可以尝试使用UI控件。但不是
所有UI控件都可以在这个游戏中使用。我可以试试shakable,但是
并非所有的shakable都是UI控件。实际上没有好方法可以在Swift 3中代表
Swift 4引入了使用任意数量的协议编写
a类的概念。

这篇关于在Swift中,如何声明符合一个或多个协议的特定类型的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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