title: Swift 3; Range 'Out of bounds'
body: <p>I've just updated Xcode to 8.0 beta 2 and swift 3.0. After updating from swift 2.3 i'm getting a lot of errors.</p>
  
  <p>I have a String-extension that's converting a Range in the "self"-string to a NSRange:</p>
  
  <pre><code>extension String {
  
      func NSRangeFromRange(_ range : Range&lt;String.Index&gt;) -&gt; NSRange {
  
          let utf16view = self.utf16
          let from = String.UTF16View.Index(range.lowerBound, within: utf16view)
          let to = String.UTF16View.Index(range.upperBound, within: utf16view)
  
          print("to: \(to) from: \(from)")
          print(self.characters.count)
  
          return NSMakeRange(utf16view.startIndex.distance(to: from), from.distance(to: to))
      //    return NSMakeRange(0, 0)    // &lt;-- removes exception
      }
  }
  </code></pre>
  
  <p>When NSMakeRange is executed I'm getting a error:</p>
  
  <blockquote>
    <p>Terminating app due to uncaught exception 'NSRangeException', reason:
    'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'</p>
  </blockquote>
  
  <p>When I'm printing the to- and from-index's, I'm getting:</p>
  
  <blockquote>
    <p>to: Index(_offset: 194) from: Index(_offset: 181)</p>
  </blockquote>
  
  <p>The character-count of the String is <code>210</code>, which seems about right.</p>
  
  <p><strong>So, I don't get why it's telling me that the index's are out of bounds, when they are less that the total count.</strong> </p>
  
  <p>This line was working perfectly before I updated to swift 3. Back then it was looking like this:</p>
  
  <pre><code>return NSMakeRange(utf16view.startIndex.distanceTo(from), from.distanceTo(to))
  </code></pre>
  
  <p>The auto-converter didn't update the syntax from swift 2.3 to 3.0, I did that myselves..</p>
  
  <p><strong>Any clues?</strong></p>