我有以下代码,其中我从 NSMutableAttributedString
继承了一个类,当我在我的类的方法中调用 append 方法时,应用程序崩溃了。我只是想了解原因。谁能帮帮我?
class Str: NSMutableAttributedString {
override init() {
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func getStr(s:String) {
self.append(NSMutableAttributedString.init(string: s))
print(self)
}
}
错误信息是:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -string only defined for abstract class. Define -[string.str string]!'
最佳答案
尝试使用这段代码在属性字符串中追加字符串,希望对您有所帮助。
func getStr(_ newstr:String, attrib:[NSAttributedString.Key: Any]) {
let newText = NSMutableAttributedString(string: newstr, attributes: attrib)
self.append(newText)
print(self)
}
您应该像下面这样传递 newstr 和 attrib。
let newstr: newstr = "your text here"
let attrib: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.black, .font: UIFont.systemFont(ofSize: 15)]
关于ios - 调用 NSMutableAttributedString 的子类中的追加时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53668465/