Swift var符合具有associateType的协议
我制定了一个协议,该协议具有Swift var符合具有associateType的协议,swift,generics,protocols,Swift,Generics,Protocols,我制定了一个协议,该协议具有关联类型 public protocol HBPrerollProtocol: NSObjectProtocol { associatedtype HBContentType func set(content: HBContentType, startImmediately: Bool) // set configuration and begin } 我试图创建一个视图,它有一个符合上述协议的属性 open class HBPrerollPlay
关联类型
public protocol HBPrerollProtocol: NSObjectProtocol {
associatedtype HBContentType
func set(content: HBContentType, startImmediately: Bool) // set configuration and begin
}
我试图创建一个视图,它有一个符合上述协议的属性
open class HBPrerollPlayerView: HBPlayerView {
open var preroll: HBPrerollProtocol?
}
但是,这不起作用,因为协议具有associateType
。错误如下:
协议“HBPrerollProtocol”只能用作一般约束,因为它具有自身或关联的类型要求
因此,我尝试创建一个符合HBPrerollProtocol
的视图,并使var就是这个视图
class HBPrerollView<T>: UIView, HBPrerollProtocol {
typealias HBContentType = T
func set(content: HBContentType, startImmediately: Bool) { }
}
HBPrerollView类:UIView,HBPrerollProtocol{
类型别名HBContentType=T
func集(内容:HBContentType,起始中间:Bool){}
}
及
开放类HBPrerollPlayerView:HBPlayerView{
打开var预滚:HBPrerollView?
}
这将导致另一个错误:
属性无法声明为打开,因为其类型使用内部类型
因为这些类位于一个单独的模块中,所以我必须将类型设置为泛型,以便将这些类用于不同的模块
我的任务是:
关联类型
T
打开或公开你在找这样的东西吗
public protocol HBPrerollProtocol: NSObjectProtocol {
associatedtype HBContentType
func set(content: HBContentType, startImmediately: Bool) // set configuration and begin
}
open class HBPrerollPlayerView<T: HBPrerollProtocol>: HBPlayerView {
open var preroll: T?
}
公共协议HBPrerollProtocol:NSObjectProtocol{
关联类型HBContentType
func set(内容:HBContentType,开始时间:Bool)//设置配置并开始
}
开放类HBPrerollPlayerView:HBPlayerView{
打开变量预滚:T?
}
public protocol HBPrerollProtocol: NSObjectProtocol {
associatedtype HBContentType
func set(content: HBContentType, startImmediately: Bool) // set configuration and begin
}
open class HBPrerollPlayerView<T: HBPrerollProtocol>: HBPlayerView {
open var preroll: T?
}