1

I'm setting up SwiftGen in the project and want to create a nested enum of assets like shown in the docs...

image

https://github.com/SwiftGen/SwiftGen#asset-catalog

I've tried multiple ways to set up the assets catalog and different config options in the yml but I can't get the output to look like it does in the readme with multiple namespaces...

All I get is a single flat enum with all the images in it...

public enum Asset {
  public static let arrowBack = ImageAsset(name: "arrow-back")
  public static let arrowBottomRight = ImageAsset(name: "arrow-bottom-right")
  public static let arrowClockwise = ImageAsset(name: "arrow-clockwise")
  public static let arrowDouble = ImageAsset(name: "arrow-double")
  public static let arrowDown = ImageAsset(name: "arrow-down")
  public static let arrowTopRight = ImageAsset(name: "arrow-top-right")
  etc...

How do I set up SwiftGen (or the xcassets file) to generate the nested enum as shown in the docs.

Thanks

Fogmeister
  • 76,236
  • 42
  • 207
  • 306

1 Answers1

3

There are two ways to have nested enums in your Assets.swift file (generated by SwiftGen with the swift5 template).

  • Have multiple assets catalogs.

You can give different asset catalogs in input for the same output file name.

xcassets:
  inputs:
    - ./Assets/Styles.xcassets
    - ./Assets/Food.xcassets
  outputs:
    templateName: swift5
    output: ./Assets.swift
    params:
        publicAccess: true

Here the two catalogs are in the same folder then you can simplify :

xcassets:
  inputs: ./Assets
  outputs:
    templateName: swift5
    output: ./Assets.swift

The Assets.swift content will be :

enum Asset {
  enum Styles {
     ...
  }
  enum Food {
     ...
  }
}
  • Provide namespaces to folders in the asset catalogs.

In addition to this, when you add a folder in your asset catalog you can check the "provides namespace" option (in Identity and type inspector - here in my Food.xcasset file).

Provides namespace option

After that you have to specify your asset "path" when you try to access to this asset : Color("Exotic/mango")

But most importantly, Swiftgen will generate a sub-enum with the items contained in the folder.

Assets.swift file

Adrien
  • 1,579
  • 6
  • 25
  • Unrelated question, @Adrien do you know of any way to flatten the creation of the sub-enum for each folder?. so for example i'll have two files on input, and the output code will contain only the main enum without the file ones. – Mr Spring Jan 25 '23 at 15:51
  • @Mr Spring : Looking at the [list of parameters](https://github.com/SwiftGen/SwiftGen/blob/stable/Documentation/templates/xcassets/swift5.md) for the swift5 template assets I don't have the impression that this is possible. But there is a parameter to do the exact opposite of what you want: `Setting this parameter will generate an enum even if only one FileName was provided as input.` :( – Adrien Jan 25 '23 at 19:02
  • Yeah, i saw this param, thanks for checking anyway in the end i created a custom stencil for my own needs, it was much easier then it seemed :D – Mr Spring Feb 01 '23 at 15:47