Both are used to unwrapped the Optional variable. (Optional Binding)
if let
- Can not access out the scope.
- no need to return statement. But we can write
if let a = a() {
let x = b(a)
x.fn()
if let u = x.nxt() {
let ux = u.brm()
if let uxt = ux.nxt() {
perform(uxt)
}
}
}
guard let
- Early exist process from the scope
- Require score existing like return, Throw etc.
- Create a new variable those can be access out the scope.
guard let a = a() else { return }
let x = b(a)
x.fn()
guard let u = x.nxt() else { return }
let ux = u.brm()
guard let uxt = ux.nxt() else { return }
perform(uxt)
Ref.
https://stackoverflow.com/questions/32256834/swift-guard-vs-if-let
https://outofbedlam.github.io/swift/2016/03/31/guard-concise/
'iOS > Swift' 카테고리의 다른 글
[Swift 5.2] Methods - Apple Documentation (0) | 2020.03.05 |
---|---|
[Swift] ExpressibleByStringLiteral, CustomStringConvertible (0) | 2020.02.18 |
[Swift4.1] map, flapMap, compactMap (0) | 2019.12.11 |
[Swift 5.2] Properties - Apple Documentation (0) | 2019.12.08 |
[Swift5.1] Overview (0) | 2019.12.04 |