Autocompletion for Images and Colours in Xcode 15

Automatically generate colors and images resources from .xcassets and avoid typos in your code with autocompletion in Xcode 15.

Francesco Leoni

2 min read

Availability

Xcode 15

Xcode 15 offers a new really cool feature.

The ability to generate assets symbols automatically without any third-party library like R.swift or SwiftGen.

So, colors and images resources are now generated by Xcode without any additional work.

This is really handy since it avoids typos and crashes due to force-unwrap a resource.

Let's see how it works.

Color resources

First, we need to add colours as we normally would to the assets folder (e.g. Assets.xcassets), let's say we add the light.red color. Then, to access the generated resource:

// SwiftUI

Color(.lightRed)

// UIKit

UIColor(resource: .lightRed)

// AppKit

NSColor(resource: .lightRed)

Image resources

The same concept applies for images too.

So, if we add the apple_logo image, we will access this way:

/// SwiftUI

Image(.appleLogo)

/// UIKit

UIImage(resource: .appleLogo)

/// AppKit

NSImage(resource: .appleLogo)

Generate Asset Symbols Options

Disable automatic generation

The resources generation is enabled by default, if you want to disable it, you can go to Build Settings and set Generate Asset Symbols to false.

Asset symbol framework support

To choose to generate resources only for a subset of frameworks go to Build Settings and set Generate Swift Asset Symbol Framework Support to the desired set of frameworks. e.g. “UIKit AppKit” or “AppKit”.

Asset symbol extensions

You can access the color or image also as an extension of its type, eg. Color.lightRed or Image.appleLogo, for SwiftUI. This is enabled by default. If you want to disable it, go to Build Settings and set the Generate Swift Asset Symbol Extensions to false.

If you have any question about this article, feel free to email me or tweet me @franceleonidev and share your opinion.

Thank you for reading and see you in the next article!

Share this article

Related articles


How To Take an Xcode Simulator Screenshot without Shadows

See how to take a ready to use screenshot of the Xcode Simulator embedded in the device bezel without any shadow or toolbar.

1 min read

SimulatorXcode

Fix “Cycle Inside“ Error in Xcode

See how to fix the “Cycle Inside“ error in Xcode. This error happens because there is an issue with the order of scripts.

1 min read

Q&AXcodeErrors

Format Functions to Multiple Lines in Xcode 15

Make you code easier to read formatting your functions to multiple lines with the option 'Format to Multiple Lines' in Xcode 15.

2 min read

Xcode