UIImage extension failed "Ambiguous use of 'angry'"

Extending UIColor in Xcode 15/Swift 5.9 did not work for me and threw 8 errors – until I changed this…

    convenience init(resource: ImageResource) {
        self.init(named: resource.rawValue)!
    }

…to this:

    convenience init(imageAsset: ImageResource) {
        self.init(named: imageAsset.rawValue)!
    }

I hope that helps someone.

The error message was “Ambiguous use of ‘angry’”. It referenced a file (GeneratedAssetSymbols) where a second definition of angry had been generated:

static let angry = DeveloperToolsSupport.ImageResource(name: "angry", bundle: resourceBundle)

Here’s what happened: XCode 15 is automatically generating static properties from image and color assets. The feature is called “Generate Swift Asset Symbol Extensions” in Build Settings — if you want to turn it off. But if you leave it on, it’s doing more or less the same thing we want to do, so that we can skip implementing the two extensions.

Listing 17.9 can look like this, accessing the generated properties:

extension Mood {
    static let angry = Mood(name: "angry", image: .angry, color: .angryRed)
    static let confused = Mood(name: "confused", image: .confused, color: .confusedPurple)
    static let crying = Mood(name: "crying", image: .crying, color: .cryingLightBlue)
    static let goofy = Mood(name: "goofy", image: .goofy, color: .goofyOrange)
    static let happy = Mood(name: "happy", image: .happy, color: .happyTurquoise)
    static let meh = Mood(name: "meh", image: .meh, color: .mehGray)
    static let sad = Mood(name: "sad", image: .sad, color: .sadBlue)
    static let sleepy = Mood(name: "sleepy", image: .sleepy, color: .sleepyLightRed)
}