swift - 在访问 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”
解决方案
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
推荐阅读
- python - 对 log1p 数据的 cross_val,但希望结果是 log1p 的倒数(又名 np.expm1)
- css - CSS最大宽度被忽略
- python - 在 Python 中按位与 0xFFFFFFFF
- selenium - 如何使用 selenium 和 java 处理网站mercurytravels.co.in 中的“From City”和“To City”字段下拉文本字段
- ruby - 如果否则,我们如何在一行中检索两个变量
- python - 币安 api 获取 makerCommission,两个来源两个不同的结果
- java - 返回类型为 Class 的 Java 泛型
或类 - c# - 如何在 C# 的只读属性中进行两次返回?
- python - 如何摆脱 Python 中阻塞内存的变量?
- ios - NavigationLink 随机弹出并“无法呈现。请提交错误。”