ios - Disallow words less than 3 characters -
in swift code below, user chooses word , types text box, how disallow words entered if less 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 add if check if word has more 3 characters:
func isreal (word: string) -> bool { if word.characters.count >= 3 { //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 } else { return false } } this way, if word shorter 3 characters, return false, otherwise, test against uitextchecker() , return true or false respectively
edit: alternative using guard:
func isreal (word: string) -> bool { guard word.characters.count >= 3 else { return false } //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 } if guard statement not met (being word.characters.count < 3), function automatically return false
Comments
Post a Comment