1

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.

a.wip
  • 59
  • 11
  • Separate string with spaces and check if last separated part can be casted to Float, or make user to press a button to check the string if it is that specific app – Lu_ May 23 '19 at 05:51
  • @Lu_ What do you mean by "Separate string with spaces". How can this be achieved? – a.wip May 23 '19 at 05:53

2 Answers2

1

Try to look at a regular expression (REGEX).

In your case the regex should look like "([A-Za-z]\w+ [1-9]\.[1-9]*) " ( note the space at the end )

Then in your textViewDidChange, as soon as the text matches the regex you can trigger your fetching method, and considering there is a space at the end it will only trigger when the user press space as you said.

Here is a link to try your regex : https://regexr.com/

And here is an example of using regex in swift : https://stackoverflow.com/a/27880748/5464805

Olympiloutre
  • 1,837
  • 2
  • 21
  • 33
0

You can separate string and try to cast it to Float:

    let someText = "here will be text from textView 3.5"
    let arrayOfWords = someText.split(separator: " ")

    // more than two words? if not, exit
    guard arrayOfWords.count >= 2 else { return }

    // now you can check if last `word` ca be a Float
    let last = String(arrayOfWords.last ?? "")
    if let number = Float(last), let wordBefore = arrayOfWords[arrayOfWords.count - 2] {
        // here you have number
        print(number)

        // here word before
        print(wordBefore)
    }
Lu_
  • 2,357
  • 16
  • 23
  • Does this just check if there is a number in the string though? I need it to only get the numbers succeeding the "word". Example above... – a.wip May 23 '19 at 06:22
  • So build on that example, I don't know all of requirements you can check a word before that number then in if statement. – Lu_ May 23 '19 at 06:31
  • Example “Gold 3.6” or “Phosphorus 2.54”. I need the function to be able to take out a piece of data that follows this structure (Word + (number) + . + (anotherNumber)), the current func you suggested seems to only find numbers in a string. – a.wip May 23 '19 at 06:34