I want to check if the user types specific phrases in a UITextView for a science app. I have this current code that creates a string of the last 20 characters then checks if that string contains a word in the arrayOfWords which gets performed on textViewDidChange:
var arrayOfWords = [“Gold", “Phosphorus”]
func textViewDidChange(_ textField: UITextView) {
    if let selectedRange = textView.selectedTextRange {
        let cursorPosition = textView.offset(from: textView.beginningOfDocument, to: selectedRange.start)
        let startRange = cursorPosition - 20
        let result = textView.text.substring(from: startRange, to: cursorPosition)
        checkIfContains(textViewString: result)
    }}
func checkIfContains(textViewString: String)  {
    if arrayOfWords.contains(where: textViewString.contains) {
        doAction()
    }
}
I know this is a very specific function so any ideas on how I could go about achieving this would be greatly appreciated.
 
     
    