Skip to content

Instantly share code, notes, and snippets.

@jbarros35
Created April 13, 2018 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbarros35/f0dd9bb38cd4d1cc7ccc1ffdbcbefe6c to your computer and use it in GitHub Desktop.
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
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