AutoComplete TextFields in Swift


UITextField can be used to add an autocomplete feature, like in Safari, to your iOS apps to suggest a word based on the characters your user types.

AutoComplete text field is a feature that shows completion suggestions automatically while the user is typing.

Please follow below steps to show implement autocomplete textField example in your iOS app.

Step 1: Add a TextField in your UIViewController and make an outlet of textField in ViewController class. Also set delegate of your text field to self in viewdidload().



Step 2: Declare an array of suggestions element to show in textField.


Step 3: Now, use UITextField delegate “shouldChangeCharactersIn” function to handle user input in textField.


Step 4: TextField delegate method textField(_:shouldChangeCharactersIn:replacementString:) calls “autoCompleteText” method which searches the suggestion list for the first entry with the prefix matching the user input. And updates the text value with the identified suggestion. “autoCompleteText” method is defined as follows:

The method returns true if the match was found otherwise returns false. The delegate method “shouldChangeCharactersIn”  returns the inverse of this value so the text field will continue to process keystrokes when a match is not found.


Step 5 : Finally, the controller implements the delegate’s textFieldShouldReturn(_:) method to “confirm” the suggestion:


Step 6: Now, compile your project and you can see the results.

Hope this article helps you.

For any queries, please feel free to add a comment in the comments section.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


  • Jihoo
    Hello, Shikha.
    Thank you very much. It is very helpful to me.
  • Rahul
    This is not case sensitive if the text is ‘ABC’, and the user will search ‘abc’ then the ABC result will not match.
    • Shikha Melkani (Moderator)
      You can make the search case insensitive by using below extension.

      extension String {

      public func hasPrefix(_ prefix: Prefix, caseSensitive: Bool) -> Bool where Prefix : StringProtocol {

      if caseSensitive { return self.hasPrefix(prefix) }

      let prefixRange = self.range(of: prefix, options: [.anchored, .caseInsensitive])
      return prefixRange != nil

      Then your search statement will be: $0.hasPrefix(prefix, caseSensitive: false)

    • IsaSam
      Into your viewDidLoad() put this:
      textField.autocapitalizationType = .allCharacters
  • Jonas
    Thank you for this. Very interesting. And also the String extension with the caseInsensitive.
  • Mahboob
    Nice solution . Great Job
    • jitendra kumar (Moderator)
      Your Welcome
  • Şevval
    I want to suggest a mail extension. For example, when you type [email protected], it should complete as How can I do that?
    • jitendra kumar (Moderator)
      Kindly contact us at [email protected]. So that we could assist you in a better way.
  • css.php

    We've bought a Shopware Extension which is a hybrid App, which we can now offer in the Google Play Store and the IOS App Store. I'am still impressed how fast and how sharp the support team leads us through every process of the App-Store clarification. Very fast and useful response.

    Markus Walter

    Markus Walter

    Founder, Das Apartment Living

    Talk to Sales


    Live Chat
    Start a Project

      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home