指定实现协议的类[重复]

时间:2021-09-22 21:32:13

This question already has an answer here:

这个问题在这里已有答案:

Given there are 2 Protocols P1 and P2, it is possible to specify a type that conforms to both protocols, eg:

鉴于有2个协议P1和P2,可以指定符合两种协议的类型,例如:

typealias P = protocol<P1, P2>

Is there a similar way to specify a type that is kind of a class and also conforms to a protocol, e.g. something like this (which does not work):

是否有类似的方法来指定类型的类型并且也符合协议,例如像这样的东西(不起作用):

typealias P = UIView: P1

1 个解决方案

#1


0  

Unfortunately it's imposible in Swift 2.2 and won't be added in Swift 3.0. The idea that you want to create Type rule for types that inherit some class and implement protocol.

不幸的是,它在Swift 2.2中是不可能的,并且不会在Swift 3.0中添加。想要为继承某些类和实现协议的类型创建类型规则的想法。

指定实现协议的类[重复]

So it's not very common that you will have such hierarchy and will have property where you want to store one of this classes. Also in POP paradigm you should have additional protocol that give you properties that you need from UIView.

因此,您将拥有此类层次结构并且具有要存储其中一个类的属性并不常见。同样在POP范例中,您应该有其他协议,为您提供UIView所需的属性。


Maybe you have such usecase: 指定实现协议的类[重复]

也许你有这样的用例:

Then create additional class: 指定实现协议的类[重复]

然后创建其他类:

And your type will be P1Base

你的类型将是P1Base


P.S. That's why if you create @property (nonatomic) UIViewController<UITableViewDelegate> *protocolClassProperty; in Obj-c, it will be bridged as UIViewController! in Swift

附:这就是为什么你创建@property(非原子)UIViewController * protocolClassProperty;在Obj-c中,它将被桥接为UIViewController!在斯威夫特

#1


0  

Unfortunately it's imposible in Swift 2.2 and won't be added in Swift 3.0. The idea that you want to create Type rule for types that inherit some class and implement protocol.

不幸的是,它在Swift 2.2中是不可能的,并且不会在Swift 3.0中添加。想要为继承某些类和实现协议的类型创建类型规则的想法。

指定实现协议的类[重复]

So it's not very common that you will have such hierarchy and will have property where you want to store one of this classes. Also in POP paradigm you should have additional protocol that give you properties that you need from UIView.

因此,您将拥有此类层次结构并且具有要存储其中一个类的属性并不常见。同样在POP范例中,您应该有其他协议,为您提供UIView所需的属性。


Maybe you have such usecase: 指定实现协议的类[重复]

也许你有这样的用例:

Then create additional class: 指定实现协议的类[重复]

然后创建其他类:

And your type will be P1Base

你的类型将是P1Base


P.S. That's why if you create @property (nonatomic) UIViewController<UITableViewDelegate> *protocolClassProperty; in Obj-c, it will be bridged as UIViewController! in Swift

附:这就是为什么你创建@property(非原子)UIViewController * protocolClassProperty;在Obj-c中,它将被桥接为UIViewController!在斯威夫特