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
The
stringByReplacingCharactersInRange
method you're using expectsRange<String.Index>
to be passed in, notNSRange
. You can't usemisspelledRange
because it's the wrong type. The link in the possible duplicate comment (NSRange to Range<String.Index>) has examples of deriving aRange<String.Index>
from anNSRange
or castingtext
toNSString
, whosestringByReplacingCharactersInRange
method does useNSRange
.