Encodable
public extension Encodable
-
Converts the encodable object into a dictionary with
String
keys andCustomStringConvertible
values.Discussion: The
asDictionary
property is an extension onEncodable
that converts an encodable object into a dictionary representation. The resulting dictionary has string keys and values that conform to theCustomStringConvertible
protocol.The property uses JSON encoding and serialization to convert the encodable object into a dictionary. It first encodes the object using the
epiJSONEncoder
fromJSONEncoder
. Then, it deserializes the encoded data into a dictionary usingJSONSerialization
.The resulting dictionary is then transformed to ensure that all values conform to
CustomStringConvertible
.- If a value is already
CustomStringConvertible
, it is directly added to the dictionary. - If a value is encodable, it recursively converts it to a dictionary using the
asDictionary
property. - If a value cannot be converted to a
String
, its description is used instead.
Declaration
Swift
var asDictionary: [String : CustomStringConvertible]? { get }
- If a value is already