3 ms·
In this case, x is an optional, eg. Int? (if you are unfamiliar with Swift, this means either an Integer or the special value nil) "guard let x = x where x > 0
by jakobegger 11y ago
In this case, x is an optional, eg. Int? (if you are unfamiliar with Swift, this means either an Integer or the special value nil)
"guard let x = x where x > 0 else {}" first unwraps the optional (check if it is not nil), then it checks if the value is greater than 0.
This is the nice thing about "guard let ...". Below the guard statement, x is no longer an optional.
- gtirloni 11y agoAh, that makes sense. Thanks!
- misterdata 11y agoAlso, the code inside the else{} block is required to leave the current scope (e.g. return from the method, fatalError(), assert(false), etc.). In that way, 'x' will always contain a valid value after the guard statement, or statements after the guard statements will not execute.