我正在尝试为通用组件制定数据源协议,但是我不知道如何正确使用associatedtype和typealias。每当我尝试在组件中定义数据源时,都会产生编译错误:
协议“ SampleDataSource”只能用作通用约束,因为它具有“自我”或相关类型要求。”
以下是我要完成的任务的摘要:
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
}