Last active
April 13, 2018 20:58
-
-
Save garsdle/d472c01e22648f57c3f300c7f8260ecf to your computer and use it in GitHub Desktop.
Init Mapping
This file contains hidden or 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
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