Search code examples
iosswiftnsattributedstring

appendAttributedString return value of type Void


I am trying to append two NSAttributedString.

let string_1 : NSAttributedString = self.answerTextView.attributedText
let string_2 : NSAttributedString = handleHtml(i) // a function that returns a NSAttributedString
let finalStr : NSAttributedString = string_1.mutableCopy().appendAttributedString(string_2)

But I got an error at the third line:

Cannot convert value of type 'Void' (aka '()') to specified type 'NSAttributedString'

How can I solve this? Thanks


Solution

  • appendAttributedString updates the caller. It doesn't return a new string. Try the following:

    let string_1 : NSAttributedString = self.answerTextView.attributedText
    let string_2 : NSAttributedString = handleHtml(i) // a function that returns a NSAttributedString
    let tmpStr : NSMutableAttributedString = string_1.mutableCopy()
    tmpStr.appendAttributedString(string_2)
    let finalStr : NSAttributedString = tmpStr.copy()