Right now I have some code that looks like the following:
let msg: String? = myStr.removingPercentEncoding ?? nil
print("msg \(msg!)")
I really don't like the use of ! in msg! as that can possibly throw an exception, I believe.
What's the best way to call myStr.removingPercentEncoding if myStr is not nil and then unwrap msg without throwing an exception?
The proper way would be:
Here,
msgis only valid inside theifstatement and only ifmyStr.removingPercentEncodingisn'tnil.If you wish to print something if
myStr.removingPercentEncodingisnil, then you could and anelse:Read up on Optional Binding in the The Swift Programming Language book.