.contains Swift5无法获得结果

时间:2019-12-08 19:58:46

标签: swift contains resultset swift5.1

func gettinSongName(){

    let folderUrl = URL(fileURLWithPath: Bundle.main.resourcePath!)

    do {
        let songPath = try FileManager.default.contentsOfDirectory(at: folderUrl, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
        for song in songPath{
            let mySong = song.absoluteString
            if mySong.contains("mp3") {
                print(mySong)
            }
        }
    } catch  {

    }
}

编写此代码时没有结果

但是我输入.contains("a")

会得到结果
 func gettinSongName(){

    let folderUrl = URL(fileURLWithPath: Bundle.main.resourcePath!)

    do {
        let songPath = try FileManager.default.contentsOfDirectory(at: folderUrl, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
        for song in songPath{
            let mySong = song.absoluteString
            if mySong.contains("a") {
                print(mySong)
            }
        }
    } catch  {

    }
}

结果:

  

file:/// Users / xzips / Library / Developer / CoreSimulator / Devices / 637C5530-3899-4D61-8AFB-8B0EC6A52D09 / data / Containers / Bundle / Application / 5EFA5FA9-2928-409E-B80C-A0A7F0A8E423 / Bebek% 20 Ninnileri.app/_CodeSignature/   文件:/// Users / xzips / Library / Developer / CoreSimulator / Devices / 637C5530-3899-4D61-8AFB-8B0EC6A52D09 / data / Containers / Bundle / Application / 5EFA5FA9-2928-409E-B80C-A0A7F0A8E423 / Bebek%20Ninnileri。 /歌曲/   文件:/// Users / xzips / Library / Developer / CoreSimulator / Devices / 637C5530-3899-4D61-8AFB-8B0EC6A52D09 / data / Containers / Bundle / Application / 5EFA5FA9-2928-409E-B80C-A0A7F0A8E423 / Bebek%20Ninnileri。 /Base.lproj/   文件:/// Users / xzips / Library / Developer / CoreSimulator / Devices / 637C5530-3899-4D61-8AFB-8B0EC6A52D09 / data / Containers / Bundle / Application / 5EFA5FA9-2928-409E-B80C-A0A7F0A8E423 / Bebek%20Ninnileri。 /Assets.car   文件:/// Users / xzips / Library / Developer / CoreSimulator / Devices / 637C5530-3899-4D61-8AFB-8B0EC6A52D09 / data / Containers / Bundle / Application / 5EFA5FA9-2928-409E-B80C-A0A7F0A8E423 / Bebek%20Ninnileri。 /Info.plist   文件:/// Users / xzips / Library / Developer / CoreSimulator / Devices / 637C5530-3899-4D61-8AFB-8B0EC6A52D09 / data / Containers / Bundle / Application / 5EFA5FA9-2928-409E-B80C-A0A7F0A8E423 / Bebek%20Ninnileri。 / Bebek%20Ninnileri   文件:/// Users / xzips / Library / Developer / CoreSimulator / Devices / 637C5530-3899-4D61-8AFB-8B0EC6A52D09 / data / Containers / Bundle / Application / 5EFA5FA9-2928-409E-B80C-A0A7F0A8E423 / Bebek%20Ninnileri。 / PkgInfo

1 个答案:

答案 0 :(得分:0)

Bundle中用于获取特定扩展名的所有资源的专用API是

func gettinSongName() { 
    if let mp3Urls = Bundle.main.urls(forResourcesWithExtension "mp3", subdirectory: nil) {
        for song in mp3Urls {
            print(song)
        }
    }
}