2

This is my code

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
    }

Getting below error message in the if statement lines

Argument type 'NSAttributedString' does not conform to expected type 'StringProtocol'

Please advice how to use the String value received from other ViewController TextField(text1) and replace a attributed String in a TextView ?

Sureshtrb
  • 51
  • 11
  • You need to give more infos: What line is creating this error? What the class of the objects? Because I don't see NSAttributedString in your question, I guess that `replaceyGFName` is a NSAttributedString and not a String? It's clearly unclear. – Larme Nov 30 '17 at 09:14
  • Find the occurence ranges (with regex or loop), then use `replaceCharacters(in range: NSRange, with str: String)` on each of the ranges. `NSMutableAttributedString` doesn't have a `replacingOccurrences()` equivalent. (Edit, I removed my previous downvote because now, even if you code is messy in my personal opinion, it gives more infos and important one concerning your issue). – Larme Nov 30 '17 at 09:51
  • Thanks and may be messy and learning thro mistakes – Sureshtrb Nov 30 '17 at 12:22

1 Answers1

1

First: A StringProtocol defined by apple documentation:

A type that can represent a string as a collection of characters.

Im assuming that the variable str25 is an NSAttributedString This is the cause of your error. Ensure that the variable you pass into replacingOccurrences(of:with:) is of Type String

Another route you could take is to convert your String into an NSMutableAttributedString, this object has a var mutableString: NSMutableString { get } property that

returns The mutable string object

Then you can use this in your replacingOccurrences(of:with:) method

Chris Karani
  • 414
  • 5
  • 14
  • If I change into String, I will loose the attributes Correct? I want to retain – Sureshtrb Nov 30 '17 at 09:41
  • Yes, use the other method if you want to keep your attributes , change to `NSMutableAttributedString ` – Chris Karani Nov 30 '17 at 09:42
  • Will it be possible to correct or advice my above code? Searched/tried and not successful so dar – Sureshtrb Nov 30 '17 at 20:22
  • used this code`let attrString = NSMutableAttributedString(string: "Hello friend") attrString.mutableString.replaceOccurrencesOfString("", withString: "", options: NSStringCompareOptions.CaseInsensitiveSearch, range: NSRange(location: 0, length: attrString.length))` from [https://stackoverflow.com/questions/29635950/replace-character-in-nsmutableattributedstring] no issues now. Thanks – Sureshtrb Dec 01 '17 at 11:44