0

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
}
2
  • You need to show your Item declaration. You need to make your Item structure Equatable and check if your array contains it before appending
    – Leo Dabus
    Commented Oct 6, 2018 at 11:36
  • stackoverflow.com/a/46519116/2303865
    – Leo Dabus
    Commented Oct 6, 2018 at 11:47

2 Answers 2

0

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)
}
1
  • Thanks @Rakesha Shastri
    – pastelsdev
    Commented Oct 6, 2018 at 11:47
0

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))
}
5
  • this will iterate the whole array even if it finds the element at the first array index. You should use contains(where:) instead of getting the first result of filter and checking if it is nil
    – Leo Dabus
    Commented Oct 6, 2018 at 11:42
  • Thanks, working nice. Btw, What is the difference between above two? @Ankit Jayaswal
    – pastelsdev
    Commented Oct 6, 2018 at 11:43
  • I think filter and index-where do not follow linear search in swift, rather heap sort same as contains. Commented Oct 6, 2018 at 11:45
  • contains(where: yourPredicate) best one. Thank you @Leo Dabus and @ Ankit Jayaswal.
    – pastelsdev
    Commented Oct 6, 2018 at 11:46
  • @pasteldev filter closure gives you array of matching results where you need to check for filtered array is empty or not, whereas index-where closure provide the index of matching result and you can directly check it with nil. Commented Oct 6, 2018 at 11:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.