title: How can I prevent a user from adding too many line breaks in a UITextView in Swift 5?
body: <p>I have a <code>UITextView</code> to let users type-in comments for some audio file they have just recorded.<br>
  I would like to limit the amount of the "\n" (newline characters) they can use to 5 (i.e., the comment should be up to 5 lines long). 
  If they try going to the sixth line I would like to show an alert with a sensible message and, upon pressing the OK button in the relative action, I would like to let the user be able to edit their text.</p>
  
  <p>Delegation is already set up and implementation of the <code>optional func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -&gt; Bool</code> is what I am doing now.<br>
  The logic I put inside shows the alert correctly but then, upon clicking OK and trying to delete some characters, the method is called again and I get the alert.<br>
  I know this is due to the counter still being stuck at 5 but resetting it to 0 allows then for 9 lines or more, so it is not a solution.</p>
  
  <p>Here is the code that I tried and that is not working as intended:  </p>
  
  <pre class="lang-swift prettyprint-override"><code>func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -&gt; Bool {
      if text == characterToCheck /* \n, declared globally */ {
          characterCounter += 1 // this was also declared as a global property
      }
  
      if characterCounter &gt; 4 {
          let newlineAC = UIAlertController(title: "Too many linebreaks", message: "Please go back and make your comment fit into a maximum of 5 lines", preferredStyle: .alert)
          newlineAC.addAction(UIAlertAction(title: "OK", style: .default) { [weak self] (_) in
              let currentText = textView.text ?? ""
              guard let currentTextRange = Range(range, in: currentText) else { return }
  
              self?.comments.text = currentText.replacingOccurrences(of: "\n", with: "@ ", range: currentTextRange)
          })
          present(newlineAC, animated: true)
  
          return false
      } else {
          return true
      }
  }  
  </code></pre>
  
  <p>No error message is thrown because the code does exactly what I ask him but I'm clearly asking it in the wrong way.
  What can I do?</p>