Non-optional shown as optional on print

Since the update from Swift 2.3 to 3.0 my localiser class stopped working. I pin pointed the problem and it is do to my injection of a string into another string. However, I can't figure out why this is happening.


So what happens is that I insert an non optional string into a string. When I print out this string, it is shown as optional.


Example for playground:


open class SettingsFormItem: NSObject {
    /
    open fileprivate(set) var title: String! = "monkey"
}
let sfi = SettingsFormItem()
func setupWithSettingsFormItem(_ settingsFormItem: SettingsFormItem) {
    let test = "SETTINGS_FIELD_\(settingsFormItem.title)"
    let testThatIsCorrect = "SETTINGS_FIELD_\(settingsFormItem.title!)"
    print("Incorrect: \(test)")
    print("Correct: \(testThatIsCorrect)")
}
setupWithSettingsFormItem(sfi)


Result:

Incorrect: SETTINGS_FIELD_Optional("monkey")
Correct: SETTINGS_FIELD_monkey


As you can see, both test is non-optional as well as the var title in class SettingsFormItem, so why does the print show SETTINGS_FIELD_Optional("monkey")? If I force unwrap settingsFormItem.title! it does work.

You declare your `title` as `String!`. It's implicitly unwrapped Optional, not "non-Optional".


And in Swift 3, "implicitly unwrapped Optional" is just an Optional, marked as "implicitly unwrap this when needed". Inside a String interpolation is not a place unwrapping is needed.

Non-optional shown as optional on print
 
 
Q