[swift-users] Swift 3/Xcode 8 change: changes with string interpolation and implicitly unwrapped optionals
Hudson, Joshua
HudsonJ at wsdot.wa.gov
Wed Jul 20 15:04:45 CDT 2016
All,
I was working on converting my application from Swift 2.2 to Swift 3.
One of the things I noticed is that the following situation seems to have changed:
In Xcode 7 I have some Swift with the following pattern: I have a string property at the top of the file where it is declared as a implicitly unwrapped optional string.
Note: This is a playground example, however in my code I use this in a view controller where I know the string will be set in code, but I don’t want to set it in the required required init?(coder aDecoder: NSCoder))
As you can see when the string is interpolated and then printed it unwraps the implicitly unwrapped optional. This has been the case since early Swift and the current Xcode.
import UIKit
var str : String!
var str2 : String!
str = "Hello, playground"
str2 = "Text = \(str)” //prints “Text = Hello, playground"
However this same code in Xcode 8 with Swift 3 shows Optional(“Hello, Playground”) unless I unwrap it again.
import UIKit
var str : String!
var str2 : String!
str = "Hello, playground"
str2 = "Text = \(str)” //prints “Text = Optional(“Hello, playground”)"
str2 = "Text = \(str!)” //prints “Text = Hello, playground"
Does anyone know why I have to unwrap the variable again during interpolation in Xcode 8 with Swift 3? Is this is a possible Xcode 8 bug (I can file a radar if it is) or a known change in Swift 3?
Thanks!
Joshua Hudson
Washington State Department Of Transportation - Mobile Application Developer Lead
More information about the swift-users
mailing list