本文介绍了无法将'NSRange'(又名'_NSRange')类型的值转换为预期类型'Range< Index>'(又名'Range< String.CharacterView.Index>')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
检查字符串字符的范围时出现此错误...
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
}
推荐答案
而不是 text!
说(text!as NSString)
。
var newString = (text! as NSString).stringByReplacingCharactersInRange(range, withString: string) as? NSString
这篇关于无法将'NSRange'(又名'_NSRange')类型的值转换为预期类型'Range< Index>'(又名'Range< String.CharacterView.Index>')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!