Created
April 13, 2018 09:37
-
-
Save jbarros35/f0dd9bb38cd4d1cc7ccc1ffdbcbefe6c to your computer and use it in GitHub Desktop.
Swift a failable initializer, will fails if any of those params would nil
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 | |
let username: String | |
init?(dictionary: Dictionary<String: Any>) { | |
guard | |
let id = dictionary[“id”] as? Int, | |
let name = dictionary[“name”] as? String, | |
let username = dictionary[“username”] as? String | |
else { | |
return nil | |
} | |
self.id = id | |
self.name = name | |
self.username = username | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment