Search code examples
iosiphoneswiftoption-typeoptional-binding

Optional Character is not getting removed even after optional binding


I am setting a optional property from previous view controller while pushing but even after optional binding its printing optional character in console. Here is my code

if let otp = self.confirmationCode {
    print(otp)        
}

It give output as follows:

"Optional(460207)"

What I am doing wrong ?


Solution

  • You are properly unwrapping the optional value.

    So if you still find Optional... in the output String, my guess is the original value self.confirmationCode does contain this text Optional(460207).

    This means you also need to unwrap the value somewhere else.