UITextChecker 代码未给出正确的结果
我正在使用我认为的标准方法来检查单词的有效性。我有一个由 4 部分组成的数组,将其连接起来以验证单词。 我把这个放在 SpriteKit 场景中的 touches begin func
中,但它总是给我答案“正确的拼写”,即使它不是。
奇怪的是,当我将完全相同的代码放入我的 中时,确实移动到了 view func
,因此它在游戏启动时运行,效果非常好。代码完全相同,只是我只是在代码之前创建了一个 let word = "WORD"
,而不是连接数组。所以我猜测问题一定与我的连接数组有关?
这是我的代码
let word = wordArray.joined()
let textChecker = UITextChecker()
let misspelledRange = textChecker.rangeOfMisspelledWord(
in: word, range: NSRange(0..<word.utf16.count), startingAt: 0, wrap: false, language: "en_US")
if misspelledRange.location != NSNotFound,
let guesses = textChecker.guesses(
forWordRange: misspelledRange, in: word, language: "en_US")
{
print("Guess: \(String(describing: guesses.first))") //Output is: Guess: Optional("Tester")
} else {
print("\(word) CORRECT SPELLING")
}
I am using the standard method I think to check a word for validity. I have a 4 part array that is joined to make the word being verified.
I have this placed in my touches began func
in a SpriteKit scene however it always gives me the answer "CORRECT SPELLING" even when it is not.
Curiously when I put the exact same bit of code into my did move to view func
so it runs when the game starts it works perfectly well. The code is exactly the same except instead of the joined array I just create a let word = "WORD"
before the code. So I am guessing the problem must be something to do with my joined array??
This is my code
let word = wordArray.joined()
let textChecker = UITextChecker()
let misspelledRange = textChecker.rangeOfMisspelledWord(
in: word, range: NSRange(0..<word.utf16.count), startingAt: 0, wrap: false, language: "en_US")
if misspelledRange.location != NSNotFound,
let guesses = textChecker.guesses(
forWordRange: misspelledRange, in: word, language: "en_US")
{
print("Guess: \(String(describing: guesses.first))") //Output is: Guess: Optional("Tester")
} else {
print("\(word) CORRECT SPELLING")
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
结果我的问题是 UITextChecker 不喜欢全大写!
只接受小写拼写
Turned out my issue was that UITextChecker does not like full caps!
Only accepts lower case spellings