I have an interesting problem:
class ListCache {
public func getCachedList<T: Codable>() -> [T]? {
//loads list from file cache using Codable
}
}
let's say I have class Foo:
class Foo: Codable {
var bar = ""
}
Now, I can do something like this:
let array: [Foo] = ListCache().getCachedList()
but I cannot do something like this:
var listsToLoad: [AnyClass] = [Foo.self]
let clazz = listsToLoad[0]
let array: [Codable] = ListCache().getCachedList()
Compiler gives me an error:
Cannot explicitly specialize a generic function
This means I cannot call getCachedList()
in a loop because I have to explicitly give it a class type.
Is there a way to achieve this? I've also tried using generic classes, but I pretty much end in the same point.
Edit:
I've tried creating:
class CodableClass: Codable {
}
then:
class Foo: CodableClass {
//...
}
and now compiler says clazz is undeclared:
var listsToLoad: [CodableClass.Type] = [Foo.self]
for clazz in listsToLoad {
if let array: [clazz] = ListCache().getCachedList() {
print(array.count)
}
}
I've tried clazz.Type
and clazz.self
as well.