mankee mankee - 1 month ago 5x
Swift Question

Nil value when unwrapping an optional

I'm getting the following error :

fatal error: unexpectedly found nil while unwrapping an Optional value

on the following code :

let cell = followedCollectionView.cellForItemAtIndexPath(deleteIndexPathh!) as! FeaturedCitiesCollectionViewCell

This is the debugger values for the variables involved:

enter image description here

I can't see what value it is referring too, the only optionals are
which is clearly not

Kex Kex

The cell may have not been nil when you created it but if you call cellForItemAtIndexPath you aren't always guaranteed to get a cell back, for instance if the cell isn't in view there is nothing there for the function to return. What you need to do instead is make changes to the data model. Remove the element you want to delete from the array (or wherever the data for the cells is coming from) and then refresh the table view or reload the cells you need to. In general you want to try and avoid trying to fetch a cell like that. Always go for the data model instead.