From 0ac3ba0bbb465ee3fa8b6a8f9e9a0cdeaf943d3f Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Tue, 13 Apr 2021 07:38:41 -0500 Subject: [PATCH] Change force unwraps so that we just fail on the move instead of crashing --- iOS/MasterFeed/MasterFeedViewController.swift | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/iOS/MasterFeed/MasterFeedViewController.swift b/iOS/MasterFeed/MasterFeedViewController.swift index ee332f0a0..c933c3b43 100644 --- a/iOS/MasterFeed/MasterFeedViewController.swift +++ b/iOS/MasterFeed/MasterFeedViewController.swift @@ -370,22 +370,28 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner { return IndexPath(row: 0, section: destIndexPath.section) } else { let identifier = makeIdentifier(sortedNodes[index]) - let candidateIndexPath = dataSource.indexPath(for: identifier)! - let movementAdjustment = sourceIndexPath < destIndexPath ? 1 : 0 - return IndexPath(row: candidateIndexPath.row - movementAdjustment, section: candidateIndexPath.section) + if let candidateIndexPath = dataSource.indexPath(for: identifier) { + let movementAdjustment = sourceIndexPath < destIndexPath ? 1 : 0 + return IndexPath(row: candidateIndexPath.row - movementAdjustment, section: candidateIndexPath.section) + } else { + return sourceIndexPath + } } } else { if index >= sortedNodes.count { let identifier = makeIdentifier(sortedNodes[sortedNodes.count - 1]) - let lastSortedIndexPath = dataSource.indexPath(for: identifier)! - let movementAdjustment = sourceIndexPath > destIndexPath ? 1 : 0 - return IndexPath(row: lastSortedIndexPath.row + movementAdjustment, section: lastSortedIndexPath.section) + if let lastSortedIndexPath = dataSource.indexPath(for: identifier) { + let movementAdjustment = sourceIndexPath > destIndexPath ? 1 : 0 + return IndexPath(row: lastSortedIndexPath.row + movementAdjustment, section: lastSortedIndexPath.section) + } else { + return sourceIndexPath + } } else { let movementAdjustment = sourceIndexPath < destIndexPath ? 1 : 0 let identifer = makeIdentifier(sortedNodes[index - movementAdjustment]) - return dataSource.indexPath(for: identifer)! + return dataSource.indexPath(for: identifer) ?? sourceIndexPath } }