用于iOS 8的swift编程customKeyboard中的UITextChecker使我的键盘崩溃

时间:2015-01-07 17:07:52

标签: swift ios8 custom-keyboard

这是我的代码..我不知道我哪里坏了... 我用Swift语言对此进行编码,我想实现自动校正,我正在使用UITextChecker,但是当我按空格键时键盘崩溃... 帮我!! :'(

@IBAction func spacePressed(button: UIButton) {
    (textDocumentProxy as UIKeyInput).insertText(" ")

    let myString = (self.textDocumentProxy as? UITextDocumentProxy)?.documentContextBeforeInput
    let textAsNSString = myString! as NSString

    var checker:UITextChecker = UITextChecker()
    var textLength = countElements(myString!)
    var checkRange:NSRange = NSMakeRange(0, textLength)

    var misspelledRange:NSRange = checker.rangeOfMisspelledWordInString(myString!, range: checkRange, startingAt: checkRange.location, wrap: false, language: "it_IT")

    var arrGuessed:NSArray = checker.guessesForWordRange(misspelledRange, inString: textAsNSString, language: "it_IT")!

    var correctedStr = textAsNSString.stringByReplacingCharactersInRange(misspelledRange, withString: arrGuessed.objectAtIndex(0) as String)



}

0 个答案:

没有答案