Created
November 9, 2019 16:53
-
-
Save wadheraswati/e61a3a74405c592808cd0f242e276187 to your computer and use it in GitHub Desktop.
Convert Encodable to Json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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