Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiently remove the last word from a string in Swift

Tags:

string

swift

I am trying to build an autocorrect system, so I need to be able to delete the last word typed and replace it with the correct one. My solution:

func autocorrect() {
    hasWordReadyToCorrect = false
    var wordProxy = self.textDocumentProxy as UITextDocumentProxy
    var stringOfWords = wordProxy.documentContextBeforeInput

    fullString = "Unset Value"

    if stringOfWords != nil {
        var words = stringOfWords.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
        for word in words {
            arrayOfWords += [word]
        }
        println("The last word of the array is \(arrayOfWords.last)")
        for (mistake, word) in autocorrectList {
            println("The mistake is \(mistake)")
            if mistake == arrayOfWords.last {
                fullString = word
                hasWordReadyToCorrect = true
            }
        }
        println("The corrected String is \(fullString)")

    }
}

This method is called after each keystroke, and if the space is pressed, it corrects the word. My problem comes in when the string of text becomes longer than about 20 words. It takes a while for it to fill the array each time a character is pressed, and it starts to lag to a point of not being able to use it. Is there a more efficient and elegant Swift way of writing this function? I'd appreciate any help!

like image 367
SomeGuy Avatar asked Oct 08 '14 19:10

SomeGuy


People also ask

How do I remove the last letter of a string in Swift?

Swift String dropLast() The dropLast() method removes the last character of the string.

How do I remove the last two characters from a string in Swift?

To remove the last character from a string we need to use the removeLast() method in swift.

How do I remove the last word from an array?

To remove the last word from a string: Call the split() method on the string to get an array containing the words in the string. Use the slice() method to get a portion of the array with the last word removed.

How do I remove spaces from a string in Swift?

To remove all leading whitespaces, use the following code: var filtered = "" var isLeading = true for character in string { if character. isWhitespace && isLeading { continue } else { isLeading = false filtered.


2 Answers

This doesn't answer the OP's "autocorrect" issue directly, but this is code is probably the easiest way to answer the question posed in the title:

Swift 3

let myString = "The dog jumped over a fence"
let myStringWithoutLastWord = myString.components(separatedBy: " ").dropLast().joined(separator: " ")
like image 92
Travis M. Avatar answered Oct 11 '22 05:10

Travis M.


1.

One thing, iteration isn't necessary for this:

for word in words {
    arrayOfWords += [word]
}

You can just do:

arrayOfWords += words

2.

Breaking the for loop will prevent iterating unnecessarily:

for (mistake, word) in autocorrectList {
    println("The mistake is \(mistake)")
    if mistake == arrayOfWords.last {
        fullString = word
        hasWordReadyToCorrect = true
        break; // Add this to stop iterating through 'autocorrectList'
    }
}

Or even better, forget the for-loop completely:

if let word = autocorrectList[arrayOfWords.last] {
    fullString = word
    hasWordReadyToCorrect = true
}

Ultimately what you're doing is seeing if the last word of the entered text matches any of the keys in the autocorrect list. You can just try to get the value directly using optional binding like this.

---

I'll let you know if I think of more.

like image 27
Logan Avatar answered Oct 11 '22 05:10

Logan