Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 859
hklel Avatar asked Jan 07 '23 13:01

hklel


1 Answers

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()
like image 64
rmaddy Avatar answered Jan 11 '23 18:01

rmaddy