ios - 如何将 URL 数组转换为字符串数组?

标签 ios swift

我有几个本地保存的 .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/

相关文章:

ios - 在 mac os 中使用 MBProgressHUD 显示 toast 消息

ios - Xcode:如何访问使用 Storyboard 呈现的 View Controller 的实例?

ios - 插入大约 10000 个数据大约需要 60 - 70 秒。我需要提交交易吗?

ios - 使用 AFNetworking 时如何从 Anyobject 获取值

iphone - 在 iPhone GPU 上解码视频帧

swift - 通过 Swift segue 传递数据每隔一段时间就有效

ios - 如何将操作设置为搜索的取消按钮

Swift - 创建对象数组

ios - UIDragPreviewParametersvisiblePath 自定义形状

ios - 确定 UIStackView 内容大小