swift - 从图像路径数组中删除项目
问题描述
我有一个函数可以收集 Documents 目录中的所有 .jpg 文件以及其中的所有文件夹。然后该函数将 .jpgs 的文件路径放入一个数组中。我需要做的是从数组中过滤一个文件 defaultImage.jpg。我遇到的问题是数组中的项目是 jpg 图像的路径,因此它们不是字符串。如何过滤“theArray”或“files”或“theImagePaths”变量以删除 defaultImage.jpg?我尝试获取 defaultImage.jpg 的索引,但又因为变量包含似乎不起作用的图像文件的路径。
我试过 - theArray.removeAll(where: {$0 == "defaultImage.jpg"}) 但我无法删除图像文件。
static func buildPresentationArray() -> [String]
{
let theDirectoryPath = ModelData.getDocumentsDirectory()
let fm = FileManager.default
var theArray = [String]()
theArray.removeAll()
let files = fm.enumerator(at: theDirectoryPath, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles])
let theImagePaths = files!.filter{ ($0 as AnyObject).pathExtension == "jpg"}
for theImagePath in theImagePaths
{
theArray.append((theImagePath as AnyObject).path)
}
return theArray
}
解决方案
尝试这个 :
theArray.removeAll { (image: String) -> Bool in
return image.contains("defaultImage.jpg")
}
推荐阅读
- javascript - 我如何在 redux-sagas 中进行测试
- javascript - 制作一个 findMax() 函数,类似于 Math.max()
- python - 如何覆盖 Sklearn 的 TSNE 以在管道功能中使用?
- html - 高度 100%,内部高度 100% 被前面的兄弟姐妹推动
- javascript - 使用 sweetalert 时,html 下拉菜单不反映新条目
- c++ - 为什么 tinyxml2 无法解析 Traffic Server xml 文件?
- arrays - 结构字段中的 ctypes 固定大小的 char 数组和初始化
- php - 修复 eclips php ide windows 7 中的 curl_init 未定义?
- reactjs - 将 Flask 服务器添加到 create-react-app 默认应用程序,/dist/bundle.js 上的 404
- html - 表单操作和 onclick 按钮