Вы должны реализовать действие UITextField .editingChanged:
textField.addTarget(self, action: #selector(self.textFieldEditingChanged(_:)), for: .editingChanged)
и проверить текст:
@objc
func textFieldEditingChanged(_ sender: UITextField) {
guard let text = sender.text else { return }
var finishedWords = text.components(separatedBy: " ").filter({ $0 != "" })
var endOfResult = ""
if text.last == " " {
endOfResult = " "
} else if !finishedWords.isEmpty {
if finishedWords.count > 1 {
endOfResult += " "
}
endOfResult += finishedWords.last ?? ""
finishedWords.removeLast()
}
var seen = [String]()
let uniqueWords = finishedWords.filter({ word in
if seen.contains(word) {
return false
} else {
seen.append(word)
return true
}
})
let result = uniqueWords.joined(separator: " ") + endOfResult
sender.text = result
}