-4
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%20Nannileri.app /_CodeSignature/file:///Users/xzips/Library/Developer/CoreSimulator/Devices/637C5530-3899-4D61-8AFB-8B0EC6A52D09/data/Containers/Bundle/Application/5EFA5FA9-2928-409E-B80C-A0A7F0A8E423/Bebek% 20Ninnileri.app/songs/file:///Users/xzips/Library/Developer/CoreSimulator/Devices/637C5530-3899-4D61-8AFB-8B0EC6A52D09/data/Containers/Bundle/Application/5EFA5FA9-2928-409E-B80C-A0A7F0A8E423 /Bebek%20Ninnileri.app/Base.lproj/file:///Users/xzips/Library/Developer/CoreSimulator/Devices/637C5530-3899-4D61-8AFB-8B0EC6A52D09/data/Containers/Bundle/Application/5EFA5FA9-2928- 409E-B80C-A0A7F0A8E423/Bebek%20Ninnileri.app/Assets.car 文件:///Users/xzips/Library/Developer/CoreSimulator/Devices/637C5530-3899-4D61-8AFB-8B0EC6A52D09/data/Containers/Bundle/Application/5EFA5FA9-2928-409E-B80C-A0A7F0A8E423/Bebek%20Nannileri.app/Info .plist file:///Users/xzips/Library/Developer/CoreSimulator/Devices/637C5530-3899-4D61-8AFB-8B0EC6A52D09/data/Containers/Bundle/Application/5EFA5FA9-2928-409E-B80C-A0A7F0A8E423/Bebek%20Ninnileri .app/Bebek%20Ninnileri file:///Users/xzips/Library/Developer/CoreSimulator/Devices/637C5530-3899-4D61-8AFB-8B0EC6A52D09/data/Containers/Bundle/Application/5EFA5FA9-2928-409E-B80C-A0A7F0A8E423 /Bebek%20Ninnileri.app/PkgInfo///Users/xzips/Library/Developer/CoreSimulator/Devices/637C5530-3899-4D61-8AFB-8B0EC6A52D09/data/Containers/Bundle/Application/5EFA5FA9-2928-409E-B80C-A0A7F0A8E423/Bebek%20Nannileri.app/Bebek %20Ninnileri 文件:///Users/xzips/Library/Developer/CoreSimulator/Devices/637C5530-3899-4D61-8AFB-8B0EC6A52D09/data/Containers/Bundle/Application/5EFA5FA9-2928-409E-B80C-A0A7F0A8E423/Bebek%20Ninnileri .app/PkgInfo///Users/xzips/Library/Developer/CoreSimulator/Devices/637C5530-3899-4D61-8AFB-8B0EC6A52D09/data/Containers/Bundle/Application/5EFA5FA9-2928-409E-B80C-A0A7F0A8E423/Bebek%20Nannileri.app/Bebek %20Ninnileri 文件:///Users/xzips/Library/Developer/CoreSimulator/Devices/637C5530-3899-4D61-8AFB-8B0EC6A52D09/data/Containers/Bundle/Application/5EFA5FA9-2928-409E-B80C-A0A7F0A8E423/Bebek%20Ninnileri .app/PkgInfo

4

1 回答 1

0

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

func gettinSongName() { 
    if let mp3Urls = Bundle.main.urls(forResourcesWithExtension "mp3", subdirectory: nil) {
        for song in mp3Urls {
            print(song)
        }
    }
}
于 2019-12-08T21:59:38.283 回答