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
if
to check if the word has more than 3 characters:This way, if
word
is shorter than 3 characters, it will returnfalse
, otherwise, it will test against theUITextChecker()
and then returntrue
orfalse
respectivelyEDIT: Alternative using
guard
:If the
guard
statement is not met (being word.characters.count < 3), the function will automatically returnfalse