Both are used to unwrapped the Optional variable. (Optional Binding)

 

if let

  1. Can not access out the scope.
  2. 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

  1. Early exist process from the scope
  2. Require score existing like return, Throw etc.
  3. 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/

+ Recent posts