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 —

  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 😊

It’s an initiative to share snippets that leverages the great power of Swift 🤘

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
Ritesh Gupta

Ritesh Gupta

iOS Developer

More from Medium

Everyone can code Puzzles — Hypocycloid

Vertical-direction Load More functionality in UITableView

iOS library support both Cocoapods and Swift Package Manager

Five Notes about iOS Application with MVVM Architecture