My task is picking files
from iCloud
and it's url,title,etc.
,then appending into item array
. After that, I am taking each values with help of struct
and listing in tableView
.
Here, one thing I need to understand, how to validate
user picked files already exist or not into my array. If exist, I don't allow to append their file with alert
message.
// Array Declaration
var items = [Item]()
var tableArray = [Item]()
// Values appending into my array
items.append(Item(url: fileurl, title: filename, exten: fileextension, size: string))
// Tableview data load
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
let item = tableArray[indexPath.row]
if tableArray.count > 0 {
cell.name_label_util.text = item.title
cell.size_label_util.text = item.size
}
return cell
}
You can check whether Item
is already existed or not, by adding a filter on existing items
array. If result is nil
then add the new item object.
Note: I am using url
to check, it should be unique. Or replace it with unique key in Item
modal.
if items.filter({ $0.url == fileurl }).first == nil {
items.append(Item(url: fileurl, title: filename, exten: fileextension, size: string))
}
Alternatives:
if items.index(where: { $0.url == fileurl }) == nil {
items.append(Item(url: fileurl, title: filename, exten: fileextension, size: string))
}
You can use contains(where:)
to check if the array contains the element by comparing the unique properties in the class.
if !items.contains(where: {$0.url == fileUrl}) {
items.append(yourItem)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.