Skip to content

Instantly share code, notes, and snippets.

@garsdle
Last active April 13, 2018 20:58
Show Gist options
  • Save garsdle/d472c01e22648f57c3f300c7f8260ecf to your computer and use it in GitHub Desktop.
Save garsdle/d472c01e22648f57c3f300c7f8260ecf to your computer and use it in GitHub Desktop.
Init Mapping
struct User {
let id: Int
let name: String
init?(dictionary: [String: Any]) {
guard let id = dictionary["id"] as? Int else { return nil }
guard let name = dictionary["user_name"] as? String else { return nil }
self.id = id
self.name = name
}
}
do {
if let userDictionary = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] {
let user = User(dictionary: userDictionary)
//Do something with user
}
} catch {
print(error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment