Reputation: 5
How can i search an array elements into a textfield ,its textfield will be suggest when each word pressed on textField from the array only?Is it possible? with swift 3.0 and xcode 8.
Upvotes: 0
Views: 1663
Reputation: 186
try this when you type you will have a filtered array
just connect this action method to textfield didchangeevent
@IBAction func textFiledDidChange(_ textFiled : UITextField){
var arr : [String] = ["a","ab","abc"]
///replace ("ab") with textfield.text!
let filteredArr = arr.filter({$0.contains("ab")})
print(filteredArr)
/// filteredArr will contain now ["ab","abc"]
}
Upvotes: 2
Reputation: 490
This might help,
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let array = ["g","d","s","a"]
if array.contains(string) {
// Typed character is in the array
} else {
// Typed character is not in the array
}
return true
}
Upvotes: 1