Title: Nil value when unwrapping an optional
Tags: ios;swift;null;option-type
Question: 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:
I can't see what value it is referring too, the only optionals are deleteIndexPathh which is clearly not nil
Comment: `cellForItemAtIndexPath` will return nil if the cell is not visible.

Here is the accepted answer: 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.
Comment for this answer: when I'm testing this, the cell is in view. basically this code works fine when the ViewController is initially loaded, however if I perform a segue away from the viewController to a different one, and then return back to this one through another segue and attempt to delete something from my collectionView this error occurs.
Comment for this answer: My problem with calling `reloadData()` on the whole collectionView, is that my collectionView has a bunch of images, and reloading it every time I delete something from it, is a bit inefficient and consumes a bunch of user data.
Comment for this answer: I use it in 26 places in my app, you're kinda scaring me here
Comment for this answer: All your images should be cached in your model, reloading the view shouldn't cause them to get downloaded again.
Comment for this answer: Yeah it's odd it behaves like that. Maybe although they are on the screen because you segued it removed them from the visibleCells array or something. Perhaps you can check that see if it contains your cell or not. Either way I agree with @JamesP. It's all in the data model. Rethink the design and cache. Don't want to be worrying about iOS deciding if ur cell is nil or not.
Comment for this answer: Good luck :) I had a LOT of headaches with this myself last week and I'll never use cellForItemAtIndexPath again!


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:09/01/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><swift><null><option-type>', 'Title': 'Nil value when unwrapping an optional', 'ViewCount': 72, 'id': 39274124, 'user_AboutMe': None, 'user_AccountId': 6320916.0, 'user_DisplayName': 'Alk', 'user_Reputation': 4847.0, 'user_Views': 430.0, 'user_id': 4909000.0}}