title: Get range from unicode character symbols. Swift
body: <p>I use <code>textView(_: shouldChangeTextIn: replacementText:)</code> function to change the input data depending on the situation. I use range, but I can not get the Swift Range when using unicode character symbols (eg <strong>( ͡° ͜ʖ ͡°)</strong> ). Please, tell me how it can be done?</p>
  
  <pre><code> func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -&gt; Bool {
  
          let maxLenthNotReached = textView.text.count + (text.count - range.length) &lt;= maxTextLength
  
          if maxLenthNotReached {
              guard let newRange = Range(range, in: identityString) else { return false }
              identityString = identityString.replacingCharacters(in: newRange, with: text)
          }
  
          return maxLenthNotReached
      }
  </code></pre>
  
  <p><a href="https://github.com/alexanderkhitev/TestTextView" rel="nofollow noreferrer">Example project</a></p>
  
  <p>An app crash example <a href="http://take.ms/ojIJq" rel="nofollow noreferrer">http://take.ms/ojIJq</a></p>
  
  <p><strong>Update</strong>: I changed this method but I got a crash again when deleting </p>
  
  <pre><code>"entering data" ""
  "testString" "༼ つ ͡° ͜ʖ ͡° ༽つ( ͡° ͜ʖ ͡"
  "entering data" ""
  
  func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -&gt; Bool {
      debugPrint("textView.text", textView.text)
      testString = textView.text.replacingCharacters(in: Range(range, in: textView.text)!, with: text)//
      debugPrint("testString", testString)
      return true
  }
  </code></pre>
  
  <p><strong>Update 1</strong>: I entered these characters in the textView</p>
  
  <pre><code>( ͡° ͜ʖ ͡°)༼ つ ͡° ͜ʖ ͡° ༽つ
  </code></pre>
  
  <p>Then I started to delete the characters with the right to the left after the three right few symbols were deleted <code>° ༽つ</code>, and the  car emoji has left, then I can not get the range, since I put the guard and application doesn't crash, if I remove that of course there will be app crash.</p>
  
  <p>Full code </p>
  
  <pre><code>class ViewController: UIViewController {
  
      // MARK: - IBOutlets
      @IBOutlet private weak var textView: UITextView! {
          didSet {
              textView.delegate = self
              textView.text = "( ͡° ͜ʖ ͡°)༼ つ ͡° ͜ʖ ͡° ༽つ"
          }
      }
  
      // MARK: - Properties
  
      private var testString = ""
  
  }
  
  
  extension ViewController: UITextViewDelegate {
  
      func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -&gt; Bool {
          guard let newRange = Range(range, in: textView.text) else {
              return false
          }
          testString = textView.text.replacingCharacters(in: newRange, with: text)
          return true
      }
  
  }
  </code></pre>
  
  <p><strong>Update 2</strong>: After talking with Martin, I found and provided one detail that this problem only happens with the Google keyboard, and with the default keyboard everything works as expected.</p>
  
  <p>The original line I had was <code>"( ͡° ͜ʖ ͡°)༼ つ ͡° ͜ʖ ͡° ༽つ”</code>, this line is used for an example.If I start deleting this line from left to right, I get the app crash, Martin asked to show the latest data in the console before the app crashes, last print before crash is <code>textView" "( ͡° ͜ʖ ͡°)༼ つ ͡° ͜ʖ ͡" "range" {27, 1}</code></p>