首页 > 解决方案 > 从图像路径数组中删除项目

问题描述

我有一个函数可以收集 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
}

标签: swift

解决方案


尝试这个 :

theArray.removeAll { (image: String) -> Bool in
    return image.contains("defaultImage.jpg")
}

推荐阅读