我有代码:
struct FilesToDownload {
var fileInternetUrl: String?
var fileName: String?
var savedURL: String?
var productImageUrl: URL?
var fileSize: Int
}
var filesToDownload = [FilesToDownload]()
let fileToDelete = "img1000.jpg"
如何从数组中删除一个选定的文件(要删除:fileToDelete)?
答案 0 :(得分:1)
您可以尝试
filesToDownload = filesToDownload.filter { $0.fileName != fileToDelete }
答案 1 :(得分:0)
获取要删除的对象的索引并将其删除:
var filesToDownload = [FilesToDownload]()
let fileToDelete = "img1000.jpg"
// get the index or nil if the array does not contain a file with the given name
let fileToDeleteIndex = filesToDownload.index { $0.fileName == fileToDelete }
// if there is a file -> delete it
if let fileToDeleteIndex = fileToDeleteIndex {
filesToDownload.remove(at: fileToDeleteIndex)
}
答案 2 :(得分:0)
首先找到目标商品的索引并删除。简单!
if let index = filesToDownload.index(where: {$0.fileName == fileToDelete} ) {
filesToDownload.remove(at: index)
}