I am trying to create an app where the user can create words up from syllables. Is it possible to check if these words exist?
Can I check if a string is an existing word?
If this is MacOS, use NSSpellChecker
https://developer.apple.com/documentation/appkit/nsspellchecker
For IOS, use UITextChecker;
func isCorrect(word: String) -> Bool {
let checker = UITextChecker()
let range = NSRange(location: 0, length: word.utf16.count)
let misspelledRange = checker.rangeOfMisspelledWord(in: word, range: range, startingAt: 0, wrap: false, language: "en")
return misspelledRange.location == NSNotFound
}See complete explanation here:
h ttps://www.hackingwithswift.com/example-code/uikit/how-to-check-a-string-is-spelled-correctly-using-uitextchecker
tested in IOS playground :
let tested = isCorrect(word: "correct")
true
let tested = isCorrect(word: "co rrect")
false
It works nearly perfect, but it does not know some German nouns, maybe because they are not written with an upper cased letter. Is there any way to check if the word exists by ignoring if the word has an upper or lower cased letter?
you need to check for an existent word by comparing
save the words and them compare
// for save
UserDefaults.Standard.set((wordToCompare), forKey: "(namedasyoulike)")
to call
UserDefaults.Standard.string(forKey: "(storekey)")
compare
let compare1 = UserDefaults.Standard.string(forKey: "(storekey)")
let compare2 = UserDefaults.Standard.string(forKey: "(storekey2)")
them if compare1 == comapre2
{
print(words equal)
} else {
//what ever you want do
}