App crashes first time loading on iPad

The first time I tried to load my app onto my iPad I received this error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value. My app stopped in the “numberOfItemsInSection” at the array count, return array.count. I tried various if statements to check if the array was nil, but not have much success. Thanks in advance for any help!

Hi John,
we meet again on another topic :slight_smile:

You might be getting this error because the call for numberOfItemsInSection could be getting called before the array is initalised.

The only thing that can help solve this is code sample/snip or the project.




Please let me know if you need additional information. I’m guessing I need to do something in the AppDelegate.



I think this is where the problem maybe? Just not sure how to fix it.

I think Snip20180114_12

Hi John,
from the first glance, dataModel is not populated, there are no items in estimateList.

for your code not to crash, you can check using a guard or an if let to assign the value ONLY if there is a first.

Here are a couple of ways,

if dataModel.estimateList.count > 0 {
 // assign accordingly


if let firstItem = dataModel.estimateList.first {
// assign accordingly

I believe you have estimateList defines as EstimateList! because you expect it can be nil but don’t want to unwrap the optional, right? One thing that I learned (My opinion) about working with swift is that as a good practice is to use ! only for UI elements and ? for all optionals. When you master swift and know why you are using what and know the values that are passed, then you can relax this, however as good practice, refer tot he sentence above.

If you look at the doco for first on apple website, it returns an optional Element from the array, meaning Element? (points to the rule I just mentioned about when to use which). This way you will check everytime you use an optional, or use optional chaining. If that is a new term please look it up (Optional Chaining — The Swift Programming Language (Swift 5.7)), it’s an important concept.



Hi Jayant,

Thank you for the information. I have the subscription to RW tutorials and did some research on optionals and understand they can be tricky. I am still have issues with my app crashing. I am trying to create a item in each array below, am I on the right track? If it looks okay maybe I am just implementing it in the wrong spot?

Hi John (@jport1130),
I hope you got something out of the optionals tutorials/articles.

I am not sure where you are calling this code snip above. I hope it is being called before it is referred.

thereby, my suggestion on initialising a default value would be to use it as so,

if dataModel.estimateLists.count == 0 { // An array with count zero has no elements
    // Alternatively you can also use estimateLists.isEmpty which is similar

  let estimateListItem = EstimateList(estName: "Welcome", 
  ... )

  let takeOffItem = TakeOffItem(toCategory: "Welcome", ...)

And If it were me, I would have a function (Factory Pattern) that returns a estimateList Item so my initialisation code would be smaller and easier to read.

Lastly, What about the function numberOfItemsInSection , there are a lot of places that look fragile, is there a way you can share the entire project, you can post it directly to me if you don’t want to post it publicly. That way I can help you find where the problem is.



Thanks Jayant. The tutorials and articles are very helpful. Being new to this it sometimes takes me awhile to implement what I learn into my app, but I enjoy the process. How do I post he project directly to you?

This topic was automatically closed after 166 days. New replies are no longer allowed.