title: range function and crash in swift 3
body: <p>My below code crashes:</p>
  
  <pre><code>func getrange(_ from: Int, length: Int) -&gt; Range&lt;String.Index&gt;? {
      guard let fromU16 = utf16.index(utf16.startIndex, offsetBy: from, limitedBy: utf16.endIndex), fromU16 != utf16.endIndex else {
        return nil   -----&gt;crashes here
      }
      let toU16 = utf16.index(fromU16, offsetBy: length, limitedBy: utf16.endIndex) ?? utf16.endIndex
      guard let from = String.Index(fromU16, within: self),
        let to = String.Index(toU16, within: self) else { return nil }
      return from ..&lt; to
    }
  </code></pre>
  
  <p>This code is crashing with swift 3 migration.</p>
  
  <p>Can someone help debugging the issue.</p>
  
  <pre><code>Below is the sequence of events:
  
       //input for below function is: text “123456789”, string “0”, nsrange = location =9, length=0
  </code></pre>
  
  <p>1) function 1</p>
  
  <pre><code>static func numericText(_ text: String, replacedBy string: String, in nsrange: NSRange) -&gt; String {
  
              guard let range = text.range(for: nsrange) else {
                //assertionFailure("Should never reach here")
                return text.numericString()
              }
  
              // Apply Replacement String to the textField text and extract only the numeric values
              return text.replacingCharacters(in: range, with: string)
                .numericString()
            }
  </code></pre>
  
  <p>2)  function 2     </p>
  
  <pre><code>        func range(for nsrange: NSRange) -&gt; Range&lt;String.Index&gt;? {
              return range(nsrange.location, length: nsrange.length)
            }
  </code></pre>
  
  <p>3)  function 3   </p>
  
  <pre><code>        func range(_ from: Int, length: Int) -&gt; Range&lt;String.Index&gt;? {
              guard let fromU16 = utf16.index(utf16.startIndex, offsetBy: from, limitedBy: utf16.endIndex), fromU16 != utf16.endIndex else {
                return nil
              }
              let toU16 = utf16.index(fromU16, offsetBy: length, limitedBy: utf16.endIndex) ?? utf16.endIndex
              guard let from = String.Index(fromU16, within: self),
                let to = String.Index(toU16, within: self) else { return nil }
              return from ..&lt; to
            }
  </code></pre>