What is the best way to delete an object from a filtered array?

I have a search bar that allows me to filter through an array. Once I have the results of the filter I would like to be able to delete a object, if necessary. Currently when I try to delete a search result it removes it from the searchResults array, but not the items array. I assume there is a way to do this, maybe by tracking the originating index? What is the best approach to this situation. Thank you.

items = [“Tom”, “Bill”, “Ed”, “Joe”] (Ed is at Index 2 in this array)

searchResults = [“ED”] (Ed is at index 0 in this array)

Hi @jport1130,
Does your array have unique items? If so then from the filtered array you can get the value you want to remove from the array and remove it using the index(of:) method on array.

let itemsToRemove = "Ed"

if let index = items.index(of: itemToRemove) {
    items.remove(at: index)
}

Hope that helps,

Jayant

Hi Jayant,

This is the code I currently have:

@IBAction func deleteSelected() {
if let selected = collectionView.indexPathsForSelectedItems {
let items = selected.map{$0.item}.sorted().reversed()
for item in items {
print(item)
estimateList.takeOffList.remove(at: item)
}
collectionView.deleteItems(at: selected)
navigationController?.isToolbarHidden = true
}
}

My array does have unique items. When I filter the primary array the filtered array indexes change so when I try to delete my app crashes since the indexes are no longer the same. I hope this makes sense.

Hi John (@jport1130),
that is too easy to fix and a simple mistake that many make.

The code is first removing the items from the array and then trying to remove it from the collectionView. Try to first remove the items in selected from the collectionView and then remove the items from the array.

Hope that resolves your problem,

cheers,

Jayant

Hi Jayant,

The code works on the “non-filtered” array. Once I filter the array and lets say delete index[0] of the filtered array my app crashes as this same item may have been index[3] in the non-filtered array.

I will try to get more information from my app for you.

Thanks again!

Hi john (@jport1130 ),
yes that is how it is supposed to work.

Say you have 10 books (items in your array), your friend marks(selects) upto 4 books that they would like to read, so you have the index of the books they want. However one of that book is what you are wanting to read yourself, so you cannot give them that book as of now. So you want to remove that book from their selection and then give them the books they selected.

The only array (ownership) that you will modify here is your own, the master list, the one that caters as the source of all data.

Now, the problem here is that since the book you are reading is in index 8, you are trying to remove the book with the index #8 from your friends selection list which has only 4 items.

If you look at the first response with the sample code I send, I am getting the index of the item using the index(of:) function and passing the text of the item, in this example, using the title of the book from your friends list. That way you will get an index value which is between 0-3 and will not crash as opposed to getting a value between 0-9 which has the potential to crash or provide incorrect results.

Hope that helps,

Jayant

Thanks Jayant. I understand what you are saying,. I always struggle on implementation of the code you suggested into my app. I appreciate ALL the help!

Hi Jayant,

What other information can I send you to help me with this? I am at a road block and cannot get this to work for some reason (inexperiene :upside_down_face:)

Hi John,
Post the code somewhere from where I can have a look. If you could post the entire project it would be helpful to see it with all of its dependencies (if any to run the same)

cheers,

Jayant

Hi Jayant,

I am not sure how :thinking:, but I figured it out with your original response. Thanks again for ALL your help!!! Onto the next challenge.

This topic was automatically closed after 166 days. New replies are no longer allowed.