appendAttributedString返回Void类型的值

hkl*_*lel 4 nsattributedstring ios swift

我试图追加两个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)
Run Code Online (Sandbox Code Playgroud)

但我在第三行遇到错误:

无法将'Void'(又名'())类型的值转换为指定类型'NSAttributedString'

我怎么解决这个问题?谢谢

rma*_*ddy 9

appendAttributedString更新呼叫者.它不会返回新字符串.请尝试以下方法:

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()
Run Code Online (Sandbox Code Playgroud)