To create an auto-complete textfield in Swift, you can follow these steps:
Add a UITextField to your ViewController in the Storyboard.
Create an array of strings that will be the basis for your auto-completion suggestions. For example:
main.swift122 chars2 linesAdd a variable to keep track of the current suggestions:
main.swift38 chars2 linesmain.swift97 chars4 linesdataSource and delegate properties of the UITableView to the ViewController.main.swift138 chars7 linestableView(_:numberOfRowsInSection:) method to return the number of suggestions.main.swift130 chars4 linestableView(_:cellForRowAt:) method to display the suggestion.main.swift251 chars6 linestextField(_:shouldChangeCharactersIn:replacementString:) method to filter the suggestions as the user types.main.swift311 chars7 linesfilterSuggestions(_:) method to filter the suggestions and update the currentSuggestions variable.main.swift245 chars8 linesmain.swift96 chars4 linesmain.swift93 chars4 linesmain.swift147 chars4 linesAnd that's it! You should have a working auto-complete textfield in Swift.
gistlibby LogSnag