You need to use Range<String.Index>
instead of the classic NSRange
. The way I do it (maybe there is a better way) is by taking the string's String.Index
a moving it with advance
.
I don't know what range you are trying to replace, but let's pretend you want to replace the first 2 characters.
var start = textField.text.startIndex // Start at the string's start index
var end = advance(textField.text.startIndex, 2) // Take start index and advance 2 characters forward
var range: Range<String.Index> = Range<String.Index>(start: start,end: end)
textField.text.stringByReplacingCharactersInRange(range, withString: string)