"Unexpectedly found nil while unwrapping an Optional value" with UIColor

Xcode 13.0 is driving me crazy at the moment...

I've got a couple of color sets (default sRGB with "Any Appearance" & "Dark") that are used in various UIViewControllers. Two of them I also use in a custom button class:

class SpecialBorderButton:UIButton {
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        if #available(iOS 13.0, *) {
            super.traitCollectionDidChange(previousTraitCollection)
             if (traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection)) {
                 layer.borderColor = UIColor(named: "ButtonBorderColor")!.cgColor
             }
        }
    }
}

What the code does: When the theme is changed while the app is running, usually the border of a button wouldn't change until you reload the ViewController. traitCollectionDidChange is called when the theme changes and this way the border color is set properly too.

The issue navigator has been throwing the same error for 3 different UIViewControllers since I installed Xcode 13:

Failed to render and update auto layout status for ViewController (.....): The agent crashed

The crash report in Library/Logs/DiagnosticReports says:

CoreSimulator 776.3 - Device: iPhone SE (1st generation) (...simulator code...) - Runtime: iOS 15.0 (19A339) - DeviceType: iPhone SE (1st generation) myApp/SpecialButton.swift:26: Fatal error: Unexpectedly found nil while unwrapping an Optional value

First of all, I don't even use the SE 1G/iOS 15 simulator but always the SE2/iOS13 one, also, I checked one of the buttons that use that class and pressed "Debug" next to "Designables Crashed" in the Attribute Inspector and it blames the same line:

layer.borderColor = UIColor(named: "ButtonBorderColor")!.cgColor

... with the same error:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

The reason why I'm writing this question: The color exists! I checked the spelling multiple times and also copied the name but the errors are still there. I deleted derived data, cleaned the build folder and restarted Xcode and the mac multiple times but the error never fully vanish for all 3 ViewControllers. There aren't any wonky connections to that button either (as far as I've seen).

I use UIColor(named:....) with different colors in multiple other classes too (but without the custom class) and they're fine there, it's just this class that's affected, for whatever reason.

How do I fix this?

You unwrap something that may be nil:

UIColor(named: "ButtonBorderColor")

So, to avoid crash, replace by:

layer.borderColor = UIColor(named: "ButtonBorderColor")?.cgColor ?? UIColor.clear.cgColor // Or choose another default color

How did you define ButtonBorderColor ? It should be in Assets.

Read here how to create color in assets:

h t t p s : / / medium.com/developerinsider/xcode-pro-tips-how-to-use-asset-catalogs-to-support-named-colors-8959ab51fd84

Note: to be sure of the cause, add a print:

if (traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection)) {
       print("Color is",  UIColor(named: "ButtonBorderColor"))
       layer.borderColor = UIColor(named: "ButtonBorderColor")?.cgColor ?? UIColor.clear.cgColor
}
"Unexpectedly found nil while unwrapping an Optional value" with UIColor
 
 
Q