Swift UITextChecker

947 views Asked by At

I am trying to use UITextChecker in my Swift project. The code below currently has an error on the last line:

    var checker:UITextChecker = UITextChecker()
    var textLength = countElements(textView.text)
    var checkRange:NSRange = NSMakeRange(0, textLength)

    var misspelledRange:NSRange = checker.rangeOfMisspelledWordInString(textView.text, range: checkRange, startingAt: checkRange.location, wrap: false, language: "en_Us")

    var arrGuessed:NSArray = checker.guessesForWordRange(misspelledRange, inString: textView.text, language: "en_US")!

    var correctedStr = textView.text.stringByReplacingCharactersInRange(misspelledRange, withString: [arrGuessed.objectAtIndex(0)])

The error says:

'NSRange' is not convertible to 'Range<String.index>'

I am not sure where I am going wrong. Thanks

1

There are 1 answers

0
onelittlefish On

The stringByReplacingCharactersInRange method you're using expects Range<String.Index> to be passed in, not NSRange. You can't use misspelledRange because it's the wrong type. The link in the possible duplicate comment (NSRange to Range<String.Index>) has examples of deriving a Range<String.Index> from an NSRange or casting text to NSString, whose stringByReplacingCharactersInRange method does use NSRange.