title: How to split filename from file extension in Swift?
body: <p>Given the name of a file in the bundle, I want load the file into my Swift app. So I need to use this method:</p>
  
  <pre><code>let soundURL = NSBundle.mainBundle().URLForResource(fname, withExtension: ext)
  </code></pre>
  
  <p>For whatever reason, the method needs the filename separated from the file extension. Fine, it's easy enough to separate the two in most languages. But so far I'm not finding it to be so in Swift.</p>
  
  <p>So here is what I have:</p>
  
  <pre><code>var rt: String.Index = fileName.rangeOfString(".", options:NSStringCompareOptions.BackwardsSearch)
  var fname: String = fileName .substringToIndex(rt)
  var ext = fileName.substringFromIndex(rt)
  </code></pre>
  
  <p>If I don't include the typing on the first line, I get errors on the two subsequent lines. With it, I'm getting an error on the first line:</p>
  
  <pre><code>Cannot convert the expression's type '(UnicodeScalarLiteralConvertible, options: NSStringCompareOptions)' to type 'UnicodeScalarLiteralConvertible'
  </code></pre>
  
  <p>How can I split the filename from the extension? Is there some elegant way to do this?</p>
  
  <p>I was all excited about Swift because it seemed like a much more elegant language than Objective C. But now I'm finding that it has its own cumbersomeness.</p>
  
  <hr>
  
  <p>Second attempt: I decided to make my own string-search method:</p>
  
  <pre><code>func rfind(haystack: String, needle: Character) -&gt; Int {
      var a = Array(haystack)
  
      for var i = a.count - 1; i &gt;= 0; i-- {
          println(a[i])
          if a[i] == needle {
              println(i)
              return i;
          }
      }
      return -1
  }
  </code></pre>
  
  <p>But now I get an error on the line <code>var rt: String.Index = rfind(fileName, needle: ".")</code>:</p>
  
  <pre><code>'Int' is not convertible to 'String.Index'
  </code></pre>
  
  <p>Without the cast, I get an error on the two subsequent lines.</p>
  
  <p>Can anyone help me to split this filename and extension?</p>