Question: I'm allowing users to select/deselect cells in my collection view and then hit a save/delete button.

When I'm selecting rows, I add them to a dictionary 

```
  var selectedIndexes = Dictionary<IndexPath, String> ()

```

and when I deselect the row I set the selectedIndexes[indexPath] = nil

When the user hits delete I run 

```
    for index in selectedIndexes.keys {
        indexesToDelete.append(index)
    }
    collectionView.deleteItems(at: indexesToDelete)

```

This goes into the selectedIndexes dictionary, grabs the indexes, adds those indexes into an array of indexes "indexesToDelete", and then after the forloop is over, I'm deleting that array of indexes. 

When I run this I get:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (5) must be equal to the number of items contained in that section before the update (5), plus or minus the number of items inserted or deleted from that section (0 inserted, 2 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

I've printed everything out and the indexesToDelete are the correct indexes for what I'm trying to delete. I don't fully understand what the error message is saying.

Answer: 
What it is saying is that your collection view is no longer in sync with your data source. So you also need to update whatever you are using as your dataSource to reflect your changes and then call deleteCells.
```
for index in selectedIndexes.keys {
        indexesToDelete.append(index)
    }
yourCollectionViewDataSourceArray.remove(at: index)
collectionView.deleteItems(at: indexesToDelete)

```

if your collectionView data source is yourCollectionViewDataSourceArray, both are synced together. Every time collectionView reloads data it uses the the function with argument numberOfItemsInSection. When it finds out that your array.count is different than the total of items after deletion it gives you this error. Take it as a rule every time you want to update your collectionView by code, update the data array before updating it. Hope that helps