swift - Why the constant inherits the "String?" type instead of the "String?" -


in following lines of code, anothervariable constant implicitly inherits right side string type instead of string?

var variable: string? = "hello"  if let anothervariable = variable { // unwrapping   // "anothervariable" constant has string type , value "hello" } 

yes, optional unwrapping. when use if let on optional value:

  1. you enter body of condition if optional value non-nil, and
  2. the value assign of non-optional type, can use without unwrapping within scope of body of conditional.