【问题标题】:I can't get results with .contains Swift5我无法使用 .contains Swift5 获得结果
【发布时间】:2020-04-01 23:53:10
【问题描述】:
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% 20Ninnileri.app/_CodeSignature/ file:///Users/xzips/Library/Developer/CoreSimulator/Devices/637C5530-3899-4D61-8AFB-8B0EC6A52D09/data/Containers/Bundle/Application/5EFA5FA9-2928-409E-B80C-A0A7F0A8E423/Bebek%20Nannileri.app /歌曲/ file:///Users/xzips/Library/Developer/CoreSimulator/Devices/637C5530-3899-4D61-8AFB-8B0EC6A52D09/data/Containers/Bundle/Application/5EFA5FA9-2928-409E-B80C-A0A7F0A8E423/Bebek%20Nannileri.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%20Nannileri.app /资产.汽车 file:///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%20Nannileri.app /Bebek%20尼尼莱里 file:///Users/xzips/Library/Developer/CoreSimulator/Devices/637C5530-3899-4D61-8AFB-8B0EC6A52D09/data/Containers/Bundle/Application/5EFA5FA9-2928-409E-B80C-A0A7F0A8E423/Bebek%20Nannileri.app /PkgInfo

【问题讨论】:

  • 结果中没有mp3。尝试打印出所有文件,看看是否有mp3
  • 仔细检查folderUrl是否有效以及songPath是否有任何内容。
  • type contains('"m") 你会看到你是否得到了一些东西,但里面没有mp3

标签: swift contains resultset swift5.1


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多