I've been trying to research optionals in Swift all morning, and I know there are plenty of stack overflow posts, but none seem to clarify this properly.
For the following code, is this the most concise way of validating an optional?
let overlays:String = "overlays=" + self.preferences.string(forKey: "overlays")! == nil ? "" : self.preferences.string(forKey: "overlays")!
Note, I've seem it's possible to do
if( self.preferences.string(forKey:="overlays" != nil) {
//do something
}
however this is less consice and readable in my opinion.
Edit: Turns out, that doesn't even handle nil properly, as I got
"Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"
So can you provide the most concise working method to only include the string where it exists