Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert value of type 'NSRange' (aka '_NSRange') to expected type 'Range<Index>'(aka 'Range<String.CharacterView.Index>')

Tags:

ios

swift

nsrange

Getting this error when checking the range for string characters...

@objc func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let shouldChange = false
    let text = textField.text
    var newString = text!.stringByReplacingCharactersInRange(range, withString: string) as? NSString
    if newString.length > 14{
        newString = newString.substringToIndex(14)
    }
    textField.text = newString.uppercaseString

    return shouldChange
}
like image 587
mosaic6 Avatar asked Sep 24 '15 19:09

mosaic6


1 Answers

Instead of text! say (text! as NSString).

var newString = (text! as NSString).stringByReplacingCharactersInRange(range, withString: string) as? NSString
like image 77
matt Avatar answered Sep 23 '22 09:09

matt