X X X

Pageviews

Socialize - Share

Share

Wednesday, December 2, 2020

IOS Swift - guard let vs. if let

Safely Unwrapping an Optional

Optionals can be tricky to understand by simply reading the Developer's Guide documentation, but with a little in-depth review of the purpose and syntax, working with them quickly becomes second nature. Although, the XCode IDE auto-completion is good at keeping the code correct, without the full understanding it is easy to trip over the scope and references that lurk behind the syntax.

When to use "guard let"

  • … to execute a body of code when the unwrapping fails rather than when it succeeds.
  • … when you have to halt execution in the current enclosing scope if a value is incorrect or nil.
  • … when you need to know/want to log which optional didn’t have the correct value.
  • … when there is a need to unwrap multiple optionals for the same the enclosing scope.
  • … when it is likely that the number of optionals needing to be unwrapped can change.
  • … when the resulting unwrapped value must be available for the remainder of the scope and not just within a branching scope.

When to use "if let"

  • … to execute branching code when the unwrapping succeeds rather than when it fails.
  • … when you can continue execution in the current enclosing scope if a value is incorrect or nil.
  • … when your code does not care which values are nil or incorrect within the enclosing scope.
  • … to unwrap a few optionals and that number won’t likely change.
  • … the unwrapped value is only needed within a branching scope.

Four ways to unwrap an optional

  • → force unwrapping
  • Forced unwrapping is used when one is confident that the value is no-nil. When using forced unwrapping, always test the value for nil before referencing its value.

  • → optional binding
  • Optional binding occurs during an assignment to one or more constants and ensures that the scope following the assignment of the constant(s) are safe (non-nil) to use in the code. For example, during the assignment:

            if let a? = something { do_something }
            
    The 'do_something' code can safely reference the value of 'a.'

  • → implicit
  • Implicitly unwrapping is handy for contants that are not derived or 'built' and are truly constant. The value that is an optional will always use its value.

            let url: String? = "https://www.webiste.edu"!
            

  • → optional chaining
  • Simply stated, a cascading of references that require testing together for safety. Especially common for classes and structs with struct or class members that might not be safe. If the entire chain is not safe, then the following scope of code is not executed.

Notes:

  • You can also validate the resulting unwrapped optionals using multi-clause conditions.
    guard let val = optional, val op 5 {} else {return}
    
    if let val = optional, val op 5 {return} else {//Do something}
    
  • One can also unwrap multiple optionals using this syntax.

No comments:

Post a Comment

UA-42937108-2