【发布时间】:2017-11-30 08:57:48
【问题描述】:
这是我的代码
class AmDocViewController: UIViewController,UITextViewDelegate {
var text1:String!
let replaceYName: String = "text1"
var textA:String!
let replaceFName: String = "textA"
var textB:String!
let replaceyGFName: String = "textB"
@IBOutlet weak var myTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
myTextView.delegate = self;
self.view.endEditing(true)
let notes37 = NSAttributedString(string:"""
(SOME SAMPLE TEXT......)
""", attributes:[NSAttributedStringKey.foregroundColor: UIColor.gray])
let newMutableString77 = notes37.mutableCopy() as! NSMutableAttributedString
newMutableString77.append(notes37)
myTextView.attributedText = newMutableString77 as NSAttributedString
let originalString = myTextView.text
let updatedString = originalString?.replacingOccurrences(of: replaceYName, with: text1)
let updatedString1 = updatedString?.replacingOccurrences(of: replaceFName, with: textA)
if textB == "" {
let updatedString2 = updatedString1?.replacingOccurrences(of: str25, with: "N/A")
}
if textB != ""{
let updatedString2 = updatedString1?.replacingOccurrences(of: replaceyGFName, with: textB)
}
myTextView.text = updatedString2
myTextView.textAlignment = NSTextAlignment.center
}
在 if 语句行中得到以下错误消息
Argument type 'NSAttributedString' does not conform to expected type 'StringProtocol'
请建议如何使用从其他 ViewController TextField(text1) 收到的字符串值并替换 TextView 中的属性字符串?
【问题讨论】:
-
您需要提供更多信息:是哪一行造成了这个错误?对象的类别是什么?因为我在您的问题中没有看到 NSAttributedString,所以我猜
replaceyGFName是 NSAttributedString 而不是字符串?显然不清楚。 -
查找出现范围(使用正则表达式或循环),然后在每个范围上使用
replaceCharacters(in range: NSRange, with str: String)。NSMutableAttributedString没有replacingOccurrences()等效项。 (编辑,我删除了我之前的否决票,因为现在,即使您的代码在我个人看来很混乱,它也会提供更多信息和关于您的问题的重要信息)。 -
谢谢,可能会很乱,学习错误
标签: swift string uitextview nsattributedstring