title: Limiting Characters in UITextField? - Swift/Xcode
body: <p>I'm new to developing and I am trying to limit the number of characters that can be entered in a textfield, with the most recent version of Swift. I have tried to follow several different tutorials and have checked out a few different answers around the site, but I am not having much luck. </p>
  
  <p>Currently, I have this entered in my swift file:</p>
  
  <pre><code>@IBAction func InfoTextFieldLimit(sender: UITextField) {
  
          self.InfoTextField.delegate = self
  
          func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -&gt; Bool {
  
              let currentCharacterCount = textField.text?.characters.count ?? 0
              if (range.length + range.location &gt; currentCharacterCount) {
  
                  return false
              }
  
              let newLength = currentCharacterCount + string.characters.count - range.length
              return newLength &lt;= 10
          }
      }
  </code></pre>
  
  <p>...I'm not getting any errors, but nothing seems to be happening when I run the app. </p>
  
  <p>I have also tried at least 3 other methods that I didn't bother copying. (but can provide on request)</p>
  
  <p>Can anybody tell me what I'm doing wrong and point me in the right direction? Would be much appreciated! ...I feel like there's a good chance I might be overlooking something that's obvious. </p>
  
  <p>I am also going to want to do this with a textview down the road, how would that be different? </p>