index vs offset Array(0..<10).enumerated().forEach { (offset, element) in } - https://en.wikipedia.org/wiki/Zero-based_numbering - offset from the starting position
NSInternalInconsistencyException 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 (213) must be equal to the number of items contained in that section before the update (154), plus or minus the number of items inserted or deleted from that section (40 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'
1) insert 3 items at the end items.append(contentsOf: ["g", "h", "i"]) // a, b, c, d, e, f, g, h, i let indexPaths = Array(6...8).map { IndexPath(item: $0, section: 0) } collectionView.insertItems(at: indexPaths)
2) delete 3 items at the end items.removeLast() items.removeLast() items.removeLast() // a, b, c let indexPaths = Array(3...5).map { IndexPath(item: $0, section: 0) } collectionView.deleteItems(at: indexPaths)
3) update item at index 2 items[2] = " ! " // a, b, ! , d, e, f let indexPath = IndexPath(item: 2, section: 0) collectionView.reloadItems(at: [indexPath])
4) move item "c" to the end items.remove(at: 2) items.append("c") // a, b, d, e, f, c collectionView.moveItem( at: IndexPath(item: 2, section: 0), to: IndexPath(item: 5, section :0) )
! Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert item 6 into section 0, but there are only 6 items in section 0 after the update'
Ordering of Operations and Index Paths —https://developer.apple.com/library/content/ documentation/UserExperience/Conceptual/ TableView_iPhone/ManageInsertDeleteRow/ ManageInsertDeleteRow.html
Wagner–Fischer algorithm —https://en.wikipedia.org/wiki/ Wagner%E2%80%93Fischer_algorithm —https://en.wikipedia.org/wiki/ Dynamic_programming Computes the edit distance between two strings of characters.