获取位于 Assets.xcassets 的数据集中文件的路径 [英] Get path of a file in a data set located in Assets.xcassets

查看:43
本文介绍了获取位于 Assets.xcassets 的数据集中文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Assets.xcassets 中有一组音频文件:

I have a data set of audio files in my Assets.xcassets:

我正在尝试获取其中一个音频文件的路径,如下所示:

I'm trying to get the path of one of those audio files like this:

let path: String = Bundle.main.path(forResource: "acoustic_grand_piano/A4", ofType: "f32")!

但我得到一个EXC_BAD_INSTRUCTION.我试图在互联网上查看,但在数据集上找不到任何内容.

But I get a EXC_BAD_INSTRUCTION. I tried to look on the internet but I don't find anything on Data Sets.

如何获取这些文件之一的内容?

How can I get the content of one of these files?

谢谢!

推荐答案

试试这个:

  • 手动将您的文件放入一个文件夹中,并命名为您想要的任何名称.
  • 将.bundle"附加到文件夹以创建包.你会收到警告,接受它.恭喜,您刚刚创建了您的第一个捆绑包!:-)
  • 手动将该文件夹拖到您的应用中.
  • 使用以下代码获取您的文件....

  • Manually put your files into a folder, named anything you want.
  • Append ".bundle" to the folder to create a bundle. You'll get a warning, accept it. Congrats, you've just created your first bundle! :-)
  • Manually drag that folder into your app.
  • Get at your files by using the following code....

public func returnFile(_ named:String) -> String {
    let path: String = Bundle.main.path(forResource: "myAudioFiles", ofType: "bundle")! + "/" + name + ".f32"        
    do {
        return try String(contentsOfFile: path)
    }
    catch let error as NSError {
        return error.description
    }
}

现在,我的文件是 CIKernel 代码的文本文件.由于您的文件是音频文件,您可能需要将字符串返回更改为其他内容.

Now, my files are text files of CIKernel code. Since your's are audio files you may need to change the String return to something else.

就我而言,我使用的是框架,因为我希望与扩展程序和其他应用程序共享这些文件/图像.如果您在这样的设置中工作,这里是未更改的代码:

In my case I'm using a framework, as I wish to share these files/images with extensions and other apps. If you are working in such a set up, here's the unaltered code:

public func returnFile(_ resource:String, _ fileName:String, _ fileType:String) -> String {
    let identifier = "com.companyname.appname" // replace with framework bundle identifier
    let fileBundle = Bundle.init(identifier: identifier)
    let filePath = (fileBundle?.path(forResource: resource, ofType: "bundle"))! + "/" + fileName + "." + fileType
do {
    return try String(contentsOfFile: filePath)
}
catch let error as NSError {
    return error.description
}

}

这篇关于获取位于 Assets.xcassets 的数据集中文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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