iosswiftxcodeassetsasset-catalog

Xcode 16.2: Generate swift symbols for assets in package and make them public


Currently, starting with Xcode 15, it's possible to create static variables automatically for every asset item in an asset catalog, e.g. color, or image.

For example, I have a color named ColorTestInsidePackage and the package structure looks like this:

// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "MyLibrary",
    platforms: [
        .iOS(.v17),
    ],
    products: [
        .library(
            name: "MyLibrary",
            targets: ["MyLibrary"]),
    ],
    targets: [
        .target(
            name: "MyLibrary",
            resources: [
                .process("Resources")
            ]),

    ]
)

Inside the package I'm able to access the colors in the following ways:

import SwiftUI

public final class TestClass {
    let colorResource = ColorResource.colorTestInsidePackage
    let color = Color(.colorTestInsidePackage)
    let colorFromExtension = Color.colorTestInsidePackage
}

The last line is possible simply because I've forced extension generation in the Asset Catalog Settings:

enter image description here

However, when I try to access these colors from within the app, none of the generated assets are accessible and I'm getting a compilation error, simply because the extensions are created with the internal access modifier.

Question

Is there any way to make sure the compiler creates such an extension with the public access modifier, so that I could use it in in the other targets using this package (e.g. an app target or another package target)?

Use-Case

Creating a UI component library and a design system in a package, so that I could work with the colors both inside the UI component library and inside the app itself (since the colors are obviously shared) and not to import the same color asset library into the app target.


Solution

  • I don't think you can make it public,

    currently the automated functionality is set with the property:

    "generate-swift-asset-symbol-extensions" : "enabled"
    

    and seems like the new compiler/Xcode is the one that implements single-handedly that behavior.

    So, if you want to access those Colors from the main app, you'll still have to make the extension yourself:

    public extension Color {
        static var primaryColorInDesignSystem : Color = Color("primaryColorInDesignSystem", bundle: Bundle.module)
        static var secondaryColorInDesignSystem : Color = Color("secondaryColorInDesignSystem", bundle: Bundle.module)
    ...
    }