Skip to content

Instantly share code, notes, and snippets.

@dilumdesilva
Last active July 14, 2022 13:59
Show Gist options
  • Save dilumdesilva/bdc70d577363f052a3405d43c8c404ff to your computer and use it in GitHub Desktop.
Save dilumdesilva/bdc70d577363f052a3405d43c8c404ff to your computer and use it in GitHub Desktop.
Useful Swift Code Snippets
extension Encodable {
/// converts a codable model to a dictionary
var dictionary: [String: Any]? {
guard let data = try? JSONEncoder().encode(self) else { return nil }
return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? [String: Any] }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment