我想让我的代码搜索多种文件类型(mp3和wav以及flac),但我无法弄清楚如何构建它。目前我有:
for song in songPath
{
var mySong = song.absoluteString
if mySong.contains (".flac")
和
let audioPath = Bundle.main.path(forResource: songs[indexPath.row], ofType: "flac")
由于
答案 0 :(得分:1)
您可以使用Set
集合类和filter
方法:
let fileTypes = Set(["flac", "mp3", "wav"])
let filesToPlay = songPath.filter {fileTypes.contains($0.pathExtension)}
例如,如果songPath
定义如下:
let songPath = [URL(string:"audio/blah.flac")!, URL(string:"audio/fdfd.mp3")!, URL(string:"audio/ggg.ogg")!]
比print(filesToPlay)
打印:
[audio / blah.flac,audio / fdfd.mp3]