Search code examples
swiftoption-type

Swift 4 - optional word in string


I have searched this site and found similar answers but for some reason I still get Optional word printed, here's my code:

if description != nil {
    description = description! + symbol + String(describing: accumulator)
    if let desc = description {
        print(desc)
    } else {
        description = String(describing: accumulator) + symbol
        if let desc = description {
            print(desc)
        }
    }
}

The output is: Optional(value)


Solution

  • String(describing: s) will output "Optional()" if the type of s is String?

    So, you need to unwrap accumulator as well as description.