从对象数组中删除实际的可见元素

时间:2018-06-20 10:54:51

标签: ios swift

我有代码:

struct FilesToDownload {
    var fileInternetUrl: String?
    var fileName: String?
    var savedURL: String?
    var productImageUrl: URL?
    var fileSize: Int
}


var filesToDownload = [FilesToDownload]()


func startDownloadFiles(filesArray: [FilesToDownload], filesType: Int){
        for files in filesArray{
            print("- files.fileName")
            // here remove element files.fileName from array  filesToDownload
        }
    }

如何从startDownloadFiles数组中删除当前显示的元素files.fileName?

1 个答案:

答案 0 :(得分:1)

您只需在filesToDownload数组中找到与files.fileName匹配的对象的索引,然后删除该索引的元素形式

 if let index = filesToDownload.index(where: {$0.fileName.lowercased() == files.fileName.lowercased() }) {
     filesToDownload.remove(at:index)     
  }