I am currently developing an iOS application with SwiftUI. I have tried to debug the error I've gotten but cannot find what is causing it or what it even is. Though I am creating an account with Firebase at the time before the error occurs, I have determined that it is not the source of this error.
Here is the error message:
Fatal error: Unexpectedly found nil while unwrapping an Optional value: file
/BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1100.2
.280/swift/stdlib/public/core/RangeReplaceableCollection.swift, line 620
2020-01-13 21:23:24.479629-0500 Stellar[1953:919058] Fatal error: Unexpectedly found nil while
unwrapping an Optional value: file
/BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1100.2
.280/swift/stdlib/public/core/RangeReplaceableCollection.swift, line 620
What seems especially odd to me about this error message, despite the lack of resources online of anything similar, is that this seems related to swift's source code itself. I have never encountered an error message anything like this before. Any idea what is happening here or how I can go about figuring this out?
What seems especially odd to me about this error message, despite the lack of resources online of anything similar, is that this seems related to swift's source code itself.
The fatal error is coming from the standard library, which is why the associated file and line information points at the standard library source code. Your call stack would help you find out what part of your code is calling into the standard library at that point.
The error location is right here, plus or minus a little depending on how much the source has diverged from whatever version you are linking against.
Unexpectedly found nil while unwrapping an Optional value
Something is forcefully unwrapping an optional value that contains nothing. Normally you would look for a trailing ! in your code somewhere. (You can read about optionals, forced unwrapping, and implicitly unwrapped optionals starting here in The Swift Programming Language.)
However, I would assume based on the source I linked above that the trigger is the standard libraryâs unwrapping of first!. That would indicate that you likely called removeFirst() on an empty collection in violation of the methodâs preconditions.
I looked around and saw that too @SDGGiesbrecht , but I am not doing anything with any collections, so I am a bit befuddled. Here is the code it references:
@inlinable
@discardableResult
public mutating func removeFirst() -> Element {
_precondition(!isEmpty, "Can't remove first element from an empty collection")
let firstElement = first! // Crashes here
removeFirst(1)
return firstElement
}
It also seems that probably a custom collection of sorts would have caused it because isEmpty didn't trigger the precondition, despite the fact that whatever collection it was didn't have a first element which is unusual. Is it possible that this could be coming from a custom collection somewhere in SwiftUI? I mean, I can't really think of anything else that could be causing this.
It is strange that the precondition isnât firing. But there are several possibilities.
One possibility is that the source had a large section added or removed between the version you are linking against, offsetting the line number and making it so that what we are looking at has nothing to do with the problem.
Another possibility depends on how are you building your code. (You mentioned Firebase.) If you are using something besides SwiftPM and Xcode, then it is possible the standard library you are linking against was compiled with an unexpected optimization setting, causing the precondition to be eliminated.
Do you have tooling available that can show you the call stack at the instant of the crash? That is how I would go about figuring this out. Otherwise, I would look for something collectionâlike that is or might become empty, such as List, VStack, Group or the like, and experiment from there.
I feel like it's looking a bit more like a bug on SwiftUI's behalf rather than on mine, but in all honesty I have no idea. What do you think @SDGGiesbrecht? Was that what you meant when you said to try and get the call stack?
Also, from playing around a bit more, it seems the presence of this NavigationLink is causing it to crash, but still, I do not know why.
And yes, the cause is heavily involved with Appleâs frameworks. Youâll probably have to go to their developer support to get a meaningful answer. I know no more that you do at this point.
Thank you for your help @SDGGiesbrecht. It turned out that the error was being caused by the presence of a Timer in the onAppear of the view in the NavigationLink.