如何在Swift中从文件扩展名中分割文件名? [英] How to split filename from file extension in Swift?

查看:218
本文介绍了如何在Swift中从文件扩展名中分割文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于捆绑包中文件的名称,我想将文件加载到我的Swift应用程序中。所以我需要使用这个方法:

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:

let soundURL = NSBundle.mainBundle().URLForResource(fname, withExtension: ext)

无论出于何种原因,该方法需要将文件名与文件扩展名分开。很好,很容易在大多数语言中将两者分开。但到目前为止,我并没有在Swift中发现它。

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.

所以这就是我所拥有的:

So here is what I have:

var rt: String.Index = fileName.rangeOfString(".", options:NSStringCompareOptions.BackwardsSearch)
var fname: String = fileName .substringToIndex(rt)
var ext = fileName.substringFromIndex(rt)

如果我不在第一个包含输入线,我在后续两行中得到错误。有了它,我在第一行收到错误:

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:

Cannot convert the expression's type '(UnicodeScalarLiteralConvertible, options: NSStringCompareOptions)' to type 'UnicodeScalarLiteralConvertible'

如何从扩展名中拆分文件名?有没有一些优雅的方法来做到这一点?

How can I split the filename from the extension? Is there some elegant way to do this?

我对Swift感到兴奋,因为它看起来比目标C更优雅。但现在我发现了它有自己的麻烦。

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.

第二次尝试:我决定制作自己的字符串搜索方法:

Second attempt: I decided to make my own string-search method:

func rfind(haystack: String, needle: Character) -> Int {
    var a = Array(haystack)

    for var i = a.count - 1; i >= 0; i-- {
        println(a[i])
        if a[i] == needle {
            println(i)
            return i;
        }
    }
    return -1
}

但是现在我收到错误 var rt:String.Index = rfind(fileName,needle:。)

'Int' is not convertible to 'String.Index'

如果没有演员表,我会在后续的两行中收到错误。

Without the cast, I get an error on the two subsequent lines.

任何人都可以帮我分割这个文件名和扩展名吗?

Can anyone help me to split this filename and extension?

推荐答案

这是Swift 2,Xcode 7:如果你已经有扩展名的文件名,那么你可以传递完整的文件名作为第一个参数,空白字符串作为第二个参数:

This is with Swift 2, Xcode 7: If you have the filename with the extension already on it, then you can pass the full filename in as the first parameter and a blank string as the second parameter:

let soundURL = NSBundle.mainBundle()
    .URLForResource("soundfile.ext", withExtension: "")

或者nil作为扩展参数也可以。

Alternatively nil as the extension parameter also works.

如果你有一个URL,并且你想出于某种原因得到文件的名称,那么你可以这样做:

If you have a URL, and you want to get the name of the file itself for some reason, then you can do this:

soundURL.URLByDeletingPathExtension?.lastPathComponent

Swift 4

let soundURL = NSBundle.mainBundle().URLForResource("soundfile.ext", withExtension: "")
soundURL.deletingPathExtension().lastPathComponent

这篇关于如何在Swift中从文件扩展名中分割文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆