Reputation: 3470
I am trying to send and update strings inside a UITextView using and IBAction button.
the code below works fine however, every time i push the button the old text is replaced with the new one. What I am trying to do is to always append the text to the exiting one.
Any Idea?
@IBAction func equalPressed(sender: AnyObject) {
var resultString:String = "new string"
textView.text = resultString.stringByAppendingString("= " + resultLabel.text! + ";")
}
Upvotes: 0
Views: 994
Reputation: 14824
You already know how to append strings, but you're doing it two different ways. The stringByAppendingString(_:)
method is fine, but Swift's +
operator is clearer. I'd rewrite your existing method as follows:
@IBAction func equalPressed(sender: AnyObject) {
let resultString = "new string"
textView.text = resultString + "= " + resultLabel.text! + ";"
}
Then, to append the text rather than replace it, it's a simple change of including the old value in the new one:
textView.text = textView.text + resultString + "= " + resultLabel.text! + ";"
Or, using the +=
operator (x += y
is short for x = x + y
):
textView.text += resultString + "= " + resultLabel.text! + ";"
Upvotes: 1