String, substring, Range, NSRange in Swift 4 : <p>I am using the following code to get a <code>String</code> substring from an <code>NSRange</code>:</p>

<pre><code>func substring(with nsrange: NSRange) -&gt; String? {
    guard let range = Range.init(nsrange)
        else { return nil }
    let start = UTF16Index(range.lowerBound)
    let end = UTF16Index(range.upperBound)
    return String(utf16[start..&lt;end])
}
</code></pre>

<p>(via: <a href="https://mjtsai.com/blog/2016/12/19/nsregularexpression-and-swift/" rel="noreferrer">https://mjtsai.com/blog/2016/12/19/nsregularexpression-and-swift/</a>)</p>

<p>When I compile with Swift 4 (Xcode 9b4), I get the following errors for the two lines that declare <code>start</code> and <code>end</code>:</p>

<pre><code>'init' is unavailable
'init' was obsoleted in Swift 4.0
</code></pre>

<p>I am confused, since I am not using an init.</p>

<p>How can I fix this?</p>
