Weebly / Cereal

Swift object serialization
BSD 3-Clause "New" or "Revised" License
369 stars 13 forks source link

Dictionary with CER_casted() has ambiguous overloads. #21

Closed sl33k closed 8 years ago

sl33k commented 8 years ago

Hey,

i'm trying to encode a dictionary with variable value types. According to the Github Documentation one should cast the dictionary with .CER_casted() (since the value type is a protocol) before decoding/encoding. The decoding part works fine but for some reason i can't get the encoding to work. The Dictionary is of the type: [String: CerealType]

Its decoded like this: guard let data:[String:CerealType] = try decoder.decode(dataKey)?.CER_casted() else { throw CodingError.MissingData } self.data = data

and encoded like this:

try encoder.encode(self.data.CER_casted(), forKey: dataKey);

which fails with:

Ambiguous Refrence to member 'encode(_:forKey:)

Any Ideas?

-- malte

ketzusaka commented 8 years ago

Hey there,

If you're working with a protocol you'll need to use IdentifyingCerealType, and not CerealType. This is because the type needs to have an identity registered to Cereal for the lookup. You'll also need to use decoder.decodeIdentifyingCerealDictionary -- I had to do that because of the inheritance, which causes those ambiguous references.

Hope that helps, and sorry for the late response!

sl33k commented 8 years ago

Hey thanks for the answer. Just wanted to let you know that it worked perfectly. Keep up the good work 👍