【问题标题】:NSAttributedString has no member replacingOccurrences error [duplicate]NSAttributedString没有成员replaceOccurrences错误[重复]
【发布时间】: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


【解决方案1】:

第一: 苹果文档定义的 StringProtocol:

一种可以将字符串表示为字符集合的类型。

我假设变量 str25NSAttributedString 这是你的错误的原因。确保您传递给replacingOccurrences(of:with:) 的变量的类型为String

您可以采取的另一种方法是将您的String 转换为NSMutableAttributedString,此对象具有var mutableString: NSMutableString { get } 属性

返回可变字符串对象

然后你可以在你的replacingOccurrences(of:with:)方法中使用它

【讨论】:

  • 如果我改成String,我会失去属性对吗?我要保留
  • 是的,如果要保留属性,请使用其他方法,更改为NSMutableAttributedString
  • 是否可以更正或建议我的上述代码?搜索/尝试过,但没有成功
  • 使用此代码let attrString = NSMutableAttributedString(string: "Hello <b>friend<b>") attrString.mutableString.replaceOccurrencesOfString("<b>", withString: "", options: NSStringCompareOptions.CaseInsensitiveSearch, range: NSRange(location: 0, length: attrString.length)) 来自 [stackoverflow.com/questions/29635950/…,现在没有问题。谢谢
猜你喜欢
  • 2018-03-31
  • 1970-01-01
  • 2016-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2021-03-20
相关资源
最近更新 更多