Flight-School / AnyCodable

Type-erased wrappers for Encodable, Decodable, and Codable values

Home Page:https://flight.school/books/codable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Strings only work when it's a literal?

alexisbronchart opened this issue · comments

I'm trying this and getting this error:
Screenshot 2021-01-29 at 09 48 41
Is this a bug or does it work only with literals?

commented

Your example constructs a dictionary with String keys and AnyEncodable values. Like the compiler error says, you can't put a String value where an AnyEncodable value is expected. The reason string literals work is that AnyEncodable AnyEncodable conforms to ExpressibleByStringLiteral; in this context, string literals result in AnyEncodable values, not String.

You can convert a String to AnyEncodable using string interpolation (as you have in the next line of your example) or through an explicit initializer (AnyEncodable(bar)).