In the Swift code below, the user chooses a word and types it into a text box, now how to disallow the words entered if less than 3 characters in length?
func isReal (word: String) -> Bool {
//return true
let checker = UITextChecker()
let range = NSMakeRange(0, word.utf16.count)
let misspelledRange = checker.rangeOfMisspelledWord(in: word, range: range, startingAt: 0, wrap: false, language: "en")
return misspelledRange.location == NSNotFound
}
You can just add an
ifto check if the word has more than 3 characters:This way, if
wordis shorter than 3 characters, it will returnfalse, otherwise, it will test against theUITextChecker()and then returntrueorfalserespectivelyEDIT: Alternative using
guard:If the
guardstatement is not met (being word.characters.count < 3), the function will automatically returnfalse