替换字符的任何想法,为什么这种崩溃:崩溃的NSAttributedString
extension NSAttributedString {
func replaceCharacters(inRange: NSRange, withString: String) -> NSAttributedString {
let mutableString = mutableCopy() as! NSMutableAttributedString
mutableString.replaceCharacters(in: inRange, with: withString)
return mutableString
}
}
let label = UILabel()
label.attributedText = NSAttributedString(string: "abcdef")
let string = label.attributedText?.replaceCharacters(inRange: NSRange(location: 1, length: 1), withString: "-")
但是,这不?
let label = UILabel()
label.attributedText = NSAttributedString(string: "abcdef")
let mutableString = label.attributedText?.mutableCopy() as! NSMutableAttributedString
mutableString.replaceCharacters(in: NSRange(location: 1, length: 1), with: "-")
let string: NSAttributedString = mutableString
PS:首先,我对第二要点做的是代码从replaceCharacters(inRange:withString:)
内从第一要点复制。
你什么信息应用程序崩溃时得到了吗? – luckyShubhra
糟糕的内存访问 –