Skip to content

Instantly share code, notes, and snippets.

@timroesner
timroesner / CompileSafeImageAssets.md
Last active April 28, 2023 09:53
Compile Safe Image Assets in Xcode

Compile Safe Image Assets

This gist was published as part of the Twitch GitHub Organization. It is distributed under the MIT license

The script below is a tool to generate code references from the Xcode asset catalog. The benefit of this is a type safe, typo safe, and compile time checked API for your Image assets. This is in contrast to the current String based UIImage API of UIKit, which does not produce compile time errors when an asset is renamed or removed from the catalog.

Setup

In order to generate the image assets during a Build Phase, you will first need to add a new file with the name ImageAssets.swift to your project. This is important as the project file should not be modified during compile time.

Pre Compiling