如何将NSRange转换为Range<String。索引>在Swift?
我想使用以下UITextFieldDelegate方法:
func textField(textField: UITextField!,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String!) -> Bool {
textField.text.stringByReplacingCharactersInRange(???, withString: string)
这与Emilie的回答相似,但是,因为你特别问如何将NSRange转换为Range<String。索引>,你会这样做:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let start = advance(textField.text.startIndex, range.location)
let end = advance(start, range.length)
let swiftRange = Range<String.Index>(start: start, end: end)
...
}