Swift snippet #9โโโthen
Monday, 6th February, 2017
You can find its Gistย here!
Whenever we have to safely unwrap an optional value, we generally have 2 options โ
if let
guard let
Both solutions are great & thereโs nothing wrong with them ๐ But I feel safely unwrapping could get little better ๐ค Thus I made an extension on Optional
& created a function then
which executes a closure having its unwrapped value only if some exists! It could be used something like this:
let name: String? = "Ritesh"
name.then { print($0) }
where $0
is the unwrapped value which you can use to your need ๐
If you are wondering about the inception of Swift-Snippets or want to checkout more such snippets, you can find them here ๐