Skip to content

Instantly share code, notes, and snippets.

@vialyx
Created April 20, 2018 12:32
Show Gist options
  • Save vialyx/cf04b634dced07ac09a1c0fd8c3c2069 to your computer and use it in GitHub Desktop.
Save vialyx/cf04b634dced07ac09a1c0fd8c3c2069 to your computer and use it in GitHub Desktop.
// MARK: guard
typealias JSON = [String: Any?]
struct Account {
let udid: String
let token: String
}
extension Account {
init?(json: JSON) {
guard let udid = json["udid"] as? String,
let token = json["token"] as? String else {
return nil
}
self.udid = udid
self.token = token
}
}
let json: JSON = ["udid": "1234-adfsdfg-12312f-asfsa", "token": nil]
let account = Account(json: json)
/*
That example demostrate how to work 'guard' construction.
'guard' has inverse logic from if statement. store variables in context to use it after condition.
Be attentive with guard. It's require more time for compile step.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment