Created
April 20, 2018 12:32
-
-
Save vialyx/cf04b634dced07ac09a1c0fd8c3c2069 to your computer and use it in GitHub Desktop.
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
// 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