我有几个本地保存的 .txt 文件,它们被加载并保存为 URL 数组,如:
func loadShortStories() {
shortStories = Bundle.main.urls(forResourcesWithExtension: "txt", subdirectory: nil)!
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationViewController = segue.destination as? ListVC {
destinationViewController.shortStories = shortStories
}
}
然后在下一个 ListVC 中,我将它们加载到 TableView 中,我有:
var shortStories: [URL] = []
我需要对 .txt 文件的特定顺序进行硬编码,但需要先将它们转换为字符串。我该怎么做?
我试过了
let shortStoriesString = shortStories.absoluteString but it does not work.
添加:
我确实删除了 cellForRowAt 方法中的 pathextension 和 lastcomponent:
shortStories[indexPath.row].deletingPathExtension().lastPathComponent
但在我这样做之前,我想对 .txt 文件加载到 TextView 中的顺序进行硬编码,因为它是随机的,我希望它们以特定的非算法方式列出,例如:
let order : [String] = [ "story7.txt", "story3.txt", "story1.txt", "story4.txt" ]
.这就是我尝试将 URL 转换为字符串的原因。
最佳答案
你可以试试
let shortStoriesString = shortStories.map {$0.lastPathComponent}
因为 shortStories
是一个数组而不是单个对象
关于ios - 如何将 URL 数组转换为字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51940174/