2017-12-20 11 views
0

associatedTypeというプロトコルを作成しました。varはassociateTypeを持つプロトコルに準拠します

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があるため、これは機能しません。エラーは以下の通りだった:

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

だから私はHBPrerollProtocolを準拠し、varは、このビューであることを確認ビューを作ってみました。

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

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

この結果異なるエラー:

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

このクラスは分離モジュールであるため、私はタイプが一般的なので、私は違うと、このクラスを使用することができますしなければなりませんモジュール。ここ

私questingは次のとおりです。

  1. はvarがassociatedTypeを持っているプロトコルに準拠する方法はありますか?

  2. そうでない場合は、ジェネリックタイプTを公開または公開するにはどうすればよいですか?

答えて

1

このようなものをお探しですか?

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? 
} 
関連する問題