首页 > 解决方案 > 在访问 URL 路径 Swift 时删除部分字符串

问题描述

我目前正在尝试在路径“/Music/Itunes/iTunes Media/Music”中创建歌曲列表,以最终将它们插入 Youtube API。我可以访问所有需要的文件。我只是在寻找一种方法来清理打印的项目,如下所示,专辑名称在歌曲名称的前面。

var test = try FileManager.default.subpathsOfDirectory (atPath: completePath)

let list = test.joined(separator: "\n")
// Attempt to delete album name  
print(list.replacingOccurrences(of: "/\(String())/" , with: ""))   

这打印

J. Cole/2014 Forest Hills Drive
J. Cole/2014 Forest Hills Drive/12 Love Yourz.mp3
J. Cole/2014 Forest Hills Drive/06 Fire Squad.mp3
J. Cole/2014 Forest Hills Drive/09 No Role Modelz.mp3

有没有办法删除那部分?为了返回示例“J. Cole 12 Love Yourz.mp3”

标签: swift

解决方案


Here's one way to filter and combine the paths you obtain from subpathsOfDirectory:

var test = try FileManager.default.subpathsOfDirectory (atPath: completePath)

var list = test.flatMap {
    let pathComps = $0.components(separatedBy: "/")
    return pathComps.count >= 3 ? pathComps[0] + " " + pathComps.last! : nil
}.joined(separator: "\n")

print(list)

Output:

J. Cole 12 Love Yourz.mp3
J. Cole 06 Fire Squad.mp3
J. Cole 09 No Role Modelz.mp3


推荐阅读