我正在尝试为通用组件制作数据源协议,但我无法弄清楚如何正确使用相关类型和类型别名。每当我尝试在组件中定义数据源时,都会出现编译错误:
协议 'SampleDataSource' 只能用作通用约束,因为它具有 Self 或关联的类型要求”
这是我想要完成的一个片段:
public protocol SampleDataSource {
associatedtype ObjectType
func objectAtIndex(index: Int) -> ObjectType
}
public class SampleComponent<T> {
typealias ObjectType = T
var dataSource: SampleDataSource? = nil // <-- This line has error
}