title: shouldChangeTextIn terminating if out of range, how to guard/check if out of range
body: <p>I am using - </p>
  
  <pre><code>shouldChangeTextIn range: NSRange  -
  </code></pre>
  
  <p>to change text in  3 labels as I am typing.
  If I change from label to label two quickly while the text is still updating I get a crash with the following error </p>
  
  <p>is there a check I can put into the function to make sure I am within range or the index exists? </p>
  
  <pre><code>2018-03-24 09:51:05.024557+0000 textinput3[24714:952788] ***     
  
  Terminating app due to uncaught exception 'NSRangeException', reason: 
  '-    [__NSCFString replaceCharactersInRange:withString:]: Range or     
  index out of bounds'
  </code></pre>
  
  <p>this is the function.</p>
  
  <pre><code>    func textView(_ textField: UITextView, shouldChangeTextIn range: NSRange, replacementText string: String) -&gt; Bool {
      if field?.tag == 1 {
          updatedText = ((field?.text as NSString?)?.replacingCharacters(in: range, with: string))!
      }
      if field?.tag == 2 {
          updatedTitleText = ((field?.text as NSString?)?.replacingCharacters(in: range, with: string))!
  
      }
      if field?.tag == 3 {            
          updatedCaptionText = ((field?.text as NSString?)?.replacingCharacters(in: range, with: string))!
  
      }
  
      return true
  }
  </code></pre>
  
  <p>(there is a slight lag between typing the characters and the characters appearing, so the crash happens if I change to another label while the previous label is still updating)</p>