Swift snippet #9 โ€” then

Ritesh Gupta
Feb 6, 2017 ยท 1 min read

Monday, 6th February, 2017

You can find its Gist here!

Whenever we have to safely unwrap an optional value, we generally have 2 options โ€”

  1. if let
  2. 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 ๐Ÿ˜Š

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and youโ€™ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer โ€” welcome home. Itโ€™s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store