0

I can't seem to understand why the following does not work:

var content : NSString = ""
let myLabel : UILabel = self.view.viewWithTag(i) as UILabel
content += String(format:"var %@ = false;", myLabel.text)

I get the following error:

NSString is not identical to 'UInt8'

3 Answers 3

1

It's because NSString class doesn't support appending string using +. It only supports if you use String. You can fix that issue by:

var content : String = ""
let myLabel : UILabel = self.view.viewWithTag(i) as UILabel
content += String(format:"var %@ = false;", myLabel.text!)
Sign up to request clarification or add additional context in comments.

1 Comment

@JeremyPope: I'm using XCode 6.1.1 In that it's not working now checked in latest beta.
0

try using String interpolation:

content += "var \(myLabel.text) = false;"

Comments

0

Instead of using format, why not use Swift's string interpolation?

content += "var \(myLabel.text!) = false;"

It doesn’t give you the numeric formatting that is so useful in NSString's format, but you are just inserting a string with no formatting required.

However the use of ! without checking always makes me twitch, so how about something like:

if let labelText = myLabel.text {
    content += "var \(labelText) = false;"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.