I want to make an enum that returns a class type that conforms to the codable protocol for Networking Layer.
enum SearchQueryType : String, CaseIterable{
case podcast
case episode
var classType: Any {
switch self {
case .podcast:
return PodcastSearchResponse.self
case .episode:
return EpisodeSearchResponse.self
}
}
static func objectForType <T> (type: String) -> T {
return self.allCases.first{$0.rawValue == type}!.classType as! T
}
}
the problem is when assigning the generic type from objectForType function I get an error says "Generic parameter 'T' could not be inferred"
func search (for query: String, type: String, sortByDate: Bool) {
let endpoint = SearchResultRequest.searchFor(query: query, type: type, sortByDate: sortByDate)
var objectType = SearchQueryType.ObjectForType(type: type)
NetworkManger.shared.callRequest(objectType: objectType , endpoint: endpoint) { (result) in
switch result{
}
}
}
I tried to return a type that conforms to the codable protocol, but I get an error from the search function that says the expected T.type is not codable.
is there any way to make a function or enum that returns a class type that works with my case?