0
votes

I made a protocol which has associatedType.

public protocol HBPrerollProtocol: NSObjectProtocol {
    associatedtype HBContentType

    func set(content: HBContentType, startImmediately: Bool) // set configuration and begin
}

And I'm trying to create a view which has a property conforms above protocol.

open class HBPrerollPlayerView: HBPlayerView {
    open var preroll: HBPrerollProtocol?
}

However this doesn't work because the protocol has associateType. The error was as below:

Protocol 'HBPrerollProtocol' can only be used as a generic constraint because it has Self or associated type requirements

So I tried to make a view which conforms HBPrerollProtocol and make the var is this view.

class HBPrerollView<T>: UIView, HBPrerollProtocol {
    typealias HBContentType = T
    func set(content: HBContentType, startImmediately: Bool) { }
}

and

open class HBPrerollPlayerView<T>: HBPlayerView {
    open var preroll: HBPrerollView<T>?
}

This result a different error:

Property cannot be declared open because its type uses an internal type

Because this classes are in a separated module I must make the type generic so I can use this classes with different modules.

My questing here is:

  1. Is there a way to make a var conforms protocol which has associatedType?

  2. If not, how can I make generic type T open or public?

1

1 Answers

1
votes

Are you looking for something like this?

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?
}