Change the type to implicitly unwrapped optional. This way you can use it like a regular non-optional value everywhere you know for sure it wouldn't be nil
(or where nil
would mean an unrecoverable error), and check for nil where it can be nil
.
Example:
let stringOrNil:String! = "foo"
if let string = stringOrNil { // used like an optional type
println(string)
}
println(stringOrNil.isEmpty) // used like a non-optional type, will crash is stringOrNil is nil
Generally you would want to avoid implicitly unwrapped optionals, except for cases where you cannot initialise objects too early for technical reasons, e.g. in the case of interface builder outlets or asynchronously initialised objects, etc.