Skip to content

Instantly share code, notes, and snippets.

@wadheraswati
Created November 9, 2019 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wadheraswati/e61a3a74405c592808cd0f242e276187 to your computer and use it in GitHub Desktop.
Save wadheraswati/e61a3a74405c592808cd0f242e276187 to your computer and use it in GitHub Desktop.
Convert Encodable to Json
extension Encodable {
func asJson() -> [String : Any]? {
do {
let data = try JSONEncoder().encode(self)
guard let dictionary = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String : AnyObject] else {
return nil
}
return dictionary
} catch {
print("json error: \(error)")
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment