Extensions On Optional String In Swift
Published in
1 min readJul 21, 2017
Earlier today I was writing some code that looked like this:
I figured there had to be a cleaner way to express this. My initial thought process was to write an extension to String
that does this. Very quickly though, I realized I need to extend Optional
, not String
.
So I came up with this:
This allows me to now write:
Much better!
Then I thought, there’s a way to make this even better for Optional<String>
specifically. So I came up with this:
And voila! I can now write:
Way better!
Originally published at gopalkri.com on July 21, 2017.