I have the following code setup (written in Swift):
protocol DataFetcherType {
init(_ mainData: String, fetchData: Bool)
}
class DataFetcher1: DataFetcherType {
required init(_ mainData: String, fetchData: Bool) {
}
}
class DataFetcher2: DataFetcherType {
required init(_ mainData: String, fetchData: Bool) {
}
}
struct Data<FetcherType: DataFetcherType> {
var mainData: String
var fetcher: DataFetcherType
init(_ mainData: String, using fetcher: FetcherType, fetchData: Bool = true) {
self.mainData = mainData
self.fetcher = FetcherType(mainData, fetchData: fetchData)
}
}
And I'm trying to instantiate Data
like so:
Data("foo", using: DataFetcher1)
But I get the error:
Type 'DataFetcher1.Type' cannot conform to 'DataFetcherType'
The same is true if I try it with DataFetcher2
.
I've been at it for hours and I feel like I've changed every possible thing, but I just can't get it to work. My initial attempt didn't use Generics, but I eventually decided that might be what I need to do. Was that wrong, or am I fundamentally missing something about how Swift works?