No way to determine if a named NSColor does not exist in bundle?

Originator:jacob
Number:rdar://41170387 Date Originated:06/15/2018
Status: Resolved:
Product:macOS + SDK Product Version:
Classification: Reproducible:
 
(Note: This was originally submitted as a code-level support incident, but they told me to file a bug report and refunded my credits).

In preparation for all of the upcoming macOS color changes (i.e., Dark Mode), I was attempting to use colors defined in an xcassets file. I reference the colors through their name like this:

NSColor(named: NSColor.Name("App Control Foreground Color"))

I would expect, if the specified color name didn't exist in any of the available resources, that this NSColor initializer would return nil (due to the optional initializer), and I would be able to write code like this:

NSColor(named: NSColor.Name("App Control Foreground Color")) ?? NSColor.textColor

Instead, if the named color does not exist, the initializer creates a non-color with an unspecified color space. This, in turn, can really just makes things get colored as transparent when in use and generates no warnings.

Steps to Reproduce:
1. Create a color using the NSColor(named:) initializer where the name specified doesn't actually exist in the bundle.
2. Observe the NSColor is not initialized as nil, despite the optional initializer, and is instead an invalid color.

Expected Results:
A 'nil' value return from my optional NSColor initializer.

Actual Results:
An NSColor object, but one with an invalid colorspace.

Version/Build:
macOS 10.13.5 
xCode 9.4

Configuration:
iMac Retina 5k, 27-inch, Late 2014

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!