NSRange -> Range

On page 21 you say that this makes it easy to convert from NSRange to Range and would be used in the textField(_:shouldChangeCharactersIn:replacementString:) method. Can you please post a specific example of how to do that? Your examples don’t make it clear how to do this.

@gargoyle Thanks very much for your question!

One possible example would be:

    func textField(_ textField: UITextField,
               shouldChangeCharactersIn range: NSRange,
               replacementString string: String) -> Bool {

  let nsString = textField.text as NSString?
  let newString = nsString?.replacingCharacters(in: range, with: string)
}

Here is a link where you can find the above solution, as well as others. :slight_smile:

I hope this helps!

All the best!

Well, since that specific method is probably the only time 99% of the people use a Range variable, it would be useful to have that example specifically shown :stuck_out_tongue:

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.