Hello,
I am a total Swift beginner, so I apologize if my question is silly. I was trying to write a function that would make a valid English word out of another word.
I'll post the code I have written so far here... it doesn't work at all, but I am hoping to get any feedback anyone is willing to provide. If you can also point me towards some references that might help me figure out how to do this that would be amazing.
func validWord(in word: String) -> String {
let letterArray = Array(word.lowercased().trimmingCharacters(in: .whitespacesAndNewlines))
var newWord = ""
let checker = UITextChecker()
let range = NSRange(location: 0, length: newWord.utf16.count)
let misspelledRange = checker.rangeOfMisspelledWord(in: newWord, range: range, startingAt: 0, wrap: false, language: "en")
var counter = 0
while misspelledRange.location == NSNotFound {
var extraArray = letterArray
while newWord.count <= letterArray.count {
let randInt = Int.random(in: 0 ..< extraArray.count)
newWord.append(extraArray[randInt])
extraArray.remove(at: randInt)
if extraArray.count == 1 {
break
}
}
counter += 1
if counter == 1000 {
break
}
}
return newWord
}
Note that I have the counter because my first while loop appears to be an infinite one so I just threw it in there to stop things.
Thanks everyone.