Dictionary with Strings:<<Class implementing protocol>>

Hi All.

I would like to create a dictionary that has String for keys and Types for values. The Types need to conform to Decodable. So I would like to write something like this:

let models:[String:Decodable.Type] = ["channel":Channel,
                                      "item":Item]

where Channel and Item are the actual classes, not instances of Channel and Item (I need to be able to call init(from:) on them).

Unfortunately, I can't figure out how to write this properly. Is it possible?

Thanks in advance.

Add .self after type.

Ah, Thanks! I tried Channel.Type, but that didn't work...

X.Type is a metatype, that is, a type of types.

Here, .self is an instance, while .Type is the type containing .self value. Similar to how Int is a type containing 39.

Generally, X.self is of type X.Type, and admit the subtyping relationships. In this case, since Channel conforms to Decodable, Channel.Type is a subtype of Decodable.Type.

Terms of Service

Privacy Policy

Cookie Policy