title: Correct behavior of shouldChangeCharactersIn method, the delegate of UITextFieldDelegate
body: <p>There two UITextField (email, password) in the Login/SIgnup controllers in my code (here is the <a href="https://github.com/AdAvAn/ShouldChangeCharactersInRange" rel="nofollow noreferrer">example</a>), so that users obviously could insert login/password to register/log in.</p>
  
  <p>In the method <code>shouldChangeCharactersIn</code> I check the data inserted by user and afterwards execute method <code>loginButtonChangeState()</code>, that activates the login button, in case data inserted are correct:</p>
  
  <ol>
  <li>Email: should be an email, checked with RegEx.</li>
  <li>Password: Should be at least 6 characters in length</li>
  </ol>
  
  <p>Here is the content of the method:</p>
  
  <pre><code>  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -&gt; Bool {
  
          if let text = textField.text,
              let textRange = Range(range, in: text) {
              let updatedText = text.replacingCharacters(in: textRange, with: string)
  
              switch textField {
                  case _ where textField.isIdentical(with: email) : state[textField] = chekMail(for: updatedText)
                  case _ where textField.isIdentical(with: password) : state[textField] = chekPass(for: updatedText)
                  default: break
              }
  
               loginButtonChangeState()
  
              print("shouldChangeCharactersIn: textField.text:\(text), replacementString: \(string)")
          }
          return true
      }
  </code></pre>
  
  <p>This code works well, but the following sequence of actions breaks it, and login button keeps active.</p>
  
  <ol>
  <li>Enter Email , e.g. t@t.com </li>
  <li>Than enter password, e.g. 123456</li>
  <li>Login button – becomes active</li>
  <li>tap/go to the address field again</li>
  <li>tap/go back to the password field</li>
  <li>tap on the keyboard backspace button à login button keeps active though it should have become inactive.</li>
  </ol>
  
  <p>The reason, why it keeps active, is because after taping backspace <code>shouldChangeCharactersIn</code> method transfers old content of the <code>UITextField</code>, which can be seen in the console log</p>
  
  <pre><code>shouldChangeCharactersIn: textField.text:, replacementString: t
  shouldChangeCharactersIn: textField.text:t, replacementString: @
  shouldChangeCharactersIn: textField.text:t@, replacementString: t
  shouldChangeCharactersIn: textField.text:t@t, replacementString: .
  shouldChangeCharactersIn: textField.text:t@t., replacementString: c
  shouldChangeCharactersIn: textField.text:t@t.c, replacementString: o
  shouldChangeCharactersIn: textField.text:t@t.co, replacementString: m
  shouldChangeCharactersIn: textField.text:, replacementString: 1
  shouldChangeCharactersIn: textField.text:1, replacementString: 2
  shouldChangeCharactersIn: textField.text:12, replacementString: 3
  shouldChangeCharactersIn: textField.text:123, replacementString: 4
  shouldChangeCharactersIn: textField.text:1234, replacementString: 5
  shouldChangeCharactersIn: textField.text:12345, replacementString: 6
  shouldChangeCharactersIn: textField.text:123456, replacementString:
  </code></pre>
  
  <p>In this row "<strong>shouldChangeCharactersIn: textField.text:123456, replacementString:</strong> " it is seen, that <code>replacementString</code> is "<em>space sighn</em>", but <code>textField.text</code> still contains "<em>123456</em>". </p>
  
  <p>Video can be seen here: <a href="https://github.com/AdAvAn/ShouldChangeCharactersInRange/raw/master/shouldChangeCharactersIn.gif" rel="nofollow noreferrer">https://github.com/AdAvAn/ShouldChangeCharactersInRange/raw/master/shouldChangeCharactersIn.gif</a></p>
  
  <p><strong>QUESTION:</strong></p>
  
  <p>How can I correct the work of the method so that “Log In” button become inactive after taping backspace on keyboard (<em>Ideally without clearing the content of the password UITextField)</em>?</p>
  
  <blockquote>
    <p>UDP: 25.03.18</p>
  </blockquote>
  
  <p>This problem only occurs for fields that have the <code>isSecureTextEntry</code> parameter true.</p>
  
  <p>So far I have not found a more beautiful solution, except to set <code>password.clearsOnBeginEditing = true</code>. This allows you to clear the contents of the password field each time it triggers func <code>textFieldDidBeginEditing (_ textField: UITextField)</code>.</p>