0

I currently have this Extension thanks to @Leo Dabus. It works perfectly for a single String, but how would I implement this same logic to an Set of Strings like var mySet = ["word", "notaword", "stillnotaword"]. In this example, I would want the function to only identify the first index as true (i.e. an English word).

extension String {
    public mutating func isEnglishWord() -> Bool {
        return UITextChecker().rangeOfMisspelledWord(in: self, range: NSRange(location: 0, length: utf16.count), startingAt: 0, wrap: false, language: "en_US").location == NSNotFound
    }
}

var myString = "word"

myString.isEnglishWord()
  • You can loop through the array and call the function on every value. Is this what you want? – Muhammad Zohaib Ehsan Mar 10 '19 at 05:52
  • Preferably, the function would print only the valid English words or perhaps identify the index location of only the valid English words. –  Mar 10 '19 at 07:04
  • @Ehsan I have too many values for that to work. –  Mar 10 '19 at 19:09
  • You have to loop through each value to check. Thats the most straight forward solution. You can improve it further once you do the simple solution. – Muhammad Zohaib Ehsan Mar 10 '19 at 19:29
  • I could use .forEach to loop through the Set. But, I don’t know how to implement UITextChecker for each value within the loop. Nor, can I figure how to properly enumerate or identify each value or index during the loop. Any more suggestions or any resources would be great. Thanks. –  Mar 10 '19 at 20:43
  • Let me add the answer. Test and let me know if it works to your expectation. – Muhammad Zohaib Ehsan Mar 11 '19 at 05:41

1 Answers1

0
let words = ["word", "notaword", "stillnotaword"]

let validWords = words.filter { word -> Bool in
    return word.isEnglishWord()
}

let wordsArray : NSArray = NSArray(array: words)
let validWordsIndexes = wordsArray.indexesOfObjects { (word, index, _) -> Bool in
    return (word as! String).isEnglishWord()
}

print(validWords)
print(validWordsIndexes)

extension String {
    public func isEnglishWord() -> Bool {
        return UITextChecker().rangeOfMisspelledWord(in: self, range: NSRange(location: 0, length: utf16.count), startingAt: 0, wrap: false, language: "en_US").location == NSNotFound
    }
}

I have added the code to print valid words and indexes of valid words. You can choose which ever suits you. Thanks

  • @Ehsan Thanks for the answer. It looks good, but I can't test it b/c Xcode keeps quitting unexpectedly for no reason. Once I figure what's wrong with Xcode and I get it running properly, then I will test your code and Accept as long as it works. –  Mar 12 '19 at 00:23
  • It kind of works. It's printing properly for identifying the valid English words. But the print for the index does not identify the index. Rather, it counts the number of times a valid English word occurs. I'm not worried about the index aspect because the print for identifying the valid English words does the trick for me. However, Xcode Playgrounds is showing me 2 errors. Both say **Value of type 'String' has no member 'isEnglishWord'** Are you getting those errors? –  Mar 12 '19 at 01:34
  • As the array is a generic type. You have to type cast it to String than call the isEnglishWord. As done in my answer. A – Muhammad Zohaib Ehsan Mar 12 '19 at 07:15
  • @Ehsan Ever since I updated my Mac with a couple Apple recommended updates, my Xcode Playground has been totally bugging out. Using your exact code, Playground is printing the proper output. But, it also shows the errors stated above. **When you run your code in Playgrounds, are you getting those errors too?** If not, then the problem is just another Xcode glitch. –  Mar 12 '19 at 14:09
  • perhaps its the xcode glitch. Because the same code compiles and runs absolutely fine on my xcode. – Muhammad Zohaib Ehsan Mar 12 '19 at 19:57
  • I'll take your word for it until I can figure out how to fix my Xcode Playground. In addition to those errors, it's constantly quitting unexpectedly. Thanks for the answer. –  Mar 12 '19 at 21:55