jack paster jack paster - 11 months ago 84
Swift Question

Swift 3.0 String error

s = "hello " + (value2!) + " yes"

Value2 is an nesting and s is the text of a uilabel. I'm trying to set the uitext of a label. I'm combining an nesting with two string values, one before value 2 and one after value 2. (hello, and yes are both those values. This model above worked in swift 2.0 but now there is an error with the "+" and Xcode is giving me an error to combine all these strings above.

Here is the error message. Can you please help me fix it.

enter image description here

Answer Source

I suspect that type of the s variable has a NSString type and the result of the expression is a String value, so you need to cast the result to NSString type.

var s: NSString = ""
var value2: NSString? = "there"

s = "hello \(value2!) yes" as NSString // output: "hello there yes"