Unknown Error Message from Swift Source Code

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.

1 Like

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 just used bt to print my stack trace:

* thread #1, queue = 'com.apple.main-thread', stop reason = Fatal error: Unexpectedly found nil while unwrapping an Optional value
    frame #0: 0x000000018f742df0 libswiftCore.dylib`_swift_runtime_on_report
    frame #1: 0x000000018f7b0ad0 libswiftCore.dylib`_swift_stdlib_reportFatalErrorInFile + 192
    frame #2: 0x000000018f49a2c0 libswiftCore.dylib`function signature specialization <Arg[1] = [Closure Propagated : closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in Swift._assertionFailure(_: Swift.StaticString, _: Swift.String, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never, Argument Types : [Swift.StaticStringSwift.UnsafeBufferPointer<Swift.UInt8>Swift.UIntSwift.UInt32]> of generic specialization <()> of Swift.String.withUTF8<A>((Swift.UnsafeBufferPointer<Swift.UInt8>) throws -> A) throws -> A + 260
    frame #3: 0x000000018f6b0ee8 libswiftCore.dylib`function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded> of Swift._assertionFailure(_: Swift.StaticString, _: Swift.String, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never + 436
    frame #4: 0x000000018f4892e4 libswiftCore.dylib`(extension in Swift):Swift.RangeReplaceableCollection.removeFirst() -> A.Element + 416
    frame #5: 0x00000001b950c3d4 SwiftUI`closure #1 () -> () in SwiftUI.StoredLocation.(beginUpdate in _EBDC911C9EE054BAE3D86F947C24B7C3)(in: __C.AGGraphRef) -> () + 140
    frame #6: 0x00000001b950c0a8 SwiftUI`SwiftUI.StoredLocation.(beginUpdate in _EBDC911C9EE054BAE3D86F947C24B7C3)(in: __C.AGGraphRef) -> () + 80
    frame #7: 0x00000001b950c038 SwiftUI`SwiftUI.StoredLocation.(BeginUpdate in _EBDC911C9EE054BAE3D86F947C24B7C3).apply(to: __C.AGGraphRef) -> () + 44
    frame #8: 0x00000001b90de584 SwiftUI`closure #1 () -> () in SwiftUI.(AsyncTransaction in _D63C4EB7F2B205694B6515509E76E98B).apply(in: __C.AGGraphRef) -> () + 188
    frame #9: 0x00000001b90db9ec SwiftUI`SwiftUI.(AsyncTransaction in _D63C4EB7F2B205694B6515509E76E98B).apply(in: __C.AGGraphRef) -> () + 76
    frame #10: 0x00000001b90db890 SwiftUI`closure #1 (__C.AGGraphRef) -> () in SwiftUI.ViewGraph.flushTransactions() -> () + 220
    frame #11: 0x00000001b90db524 SwiftUI`SwiftUI.ViewGraph.flushTransactions() -> () + 188
    frame #12: 0x00000001b94153fc SwiftUI`closure #1 () -> () in closure #1 () -> () in (extension in SwiftUI):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> () + 1632
    frame #13: 0x00000001b9414b98 SwiftUI`closure #1 () -> () in (extension in SwiftUI):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> () + 624
    frame #14: 0x00000001b9409f74 SwiftUI`(extension in SwiftUI):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> () + 456
    frame #15: 0x00000001b95857d8 SwiftUI`SwiftUI._UIHostingView.layoutSubviews() -> () + 172
    frame #16: 0x00000001b9585808 SwiftUI`@objc SwiftUI._UIHostingView.layoutSubviews() -> () + 28
    frame #17: 0x00000001863bdb1c UIKitCore`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2156
    frame #18: 0x0000000181b30a4c libobjc.A.dylib`-[NSObject performSelector:withObject:] + 68
    frame #19: 0x00000001889d3a9c QuartzCore`-[CALayer layoutSublayers] + 292
    frame #20: 0x00000001889d3eec QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 484
    frame #21: 0x00000001889e6614 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 140
    frame #22: 0x000000018892b1c4 QuartzCore`CA::Context::commit_transaction(CA::Transaction*, double) + 308
    frame #23: 0x0000000188955fd0 QuartzCore`CA::Transaction::commit() + 684
    frame #24: 0x0000000188956ba4 QuartzCore`CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 96
    frame #25: 0x0000000181d97524 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 36
    frame #26: 0x0000000181d921c4 CoreFoundation`__CFRunLoopDoObservers + 420
    frame #27: 0x0000000181d92774 CoreFoundation`__CFRunLoopRun + 1292
    frame #28: 0x0000000181d91f40 CoreFoundation`CFRunLoopRunSpecific + 480
    frame #29: 0x000000018c00f534 GraphicsServices`GSEventRunModal + 108
    frame #30: 0x0000000185f0aa60 UIKitCore`UIApplicationMain + 1940
  * frame #31: 0x00000001025a5924 Stellar`main at AppDelegate.swift:13:7
    frame #32: 0x0000000181c10e18 libdyld.dylib`start + 4

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.

if databaseUser != nil {
    NavigationLink(destination: VerificationView(user: databaseUser!), isActive: $showVerifyEmail) {
        Text("")
    }
}                    

Yes, that is the call stack.

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.

1 Like