ios - No se puede convertir el valor del tipo ''NSRange''(alias ''_NSRange'') al tipo esperado ''Range<Index>''(alias ''Range<String.CharacterView.Index>'')
swift (2)
Obteniendo este error al verificar el rango de caracteres de cadena ...
@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
}
En lugar de text!
decir (text! as NSString)
.
var newString = (text! as NSString).stringByReplacingCharactersInRange(range, withString: string) as? NSString
Swift 4
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
if let oldString = textField.text {
let newString = oldString.replacingCharacters(in: Range(range, in: oldString)!,
with: string)
// ...
}
// ...
}