I saw this closed issue: https://github.com/SwiftGen/SwiftGen/issues/571
I'd like to reopen this discussion. I am currently converting a bunch of icons to image assets, and use SwiftGen to generate an enum for all available icons.
However, I want to be able to list all available images in the icon set. The (stripped) generated code currently looks like this:
internal enum Asset {
internal enum Colors {
internal static let skyBlue = ColorAsset(name: "skyBlue")
}
internal enum Images {
internal enum MapIcons {
internal static let accessDenied = ImageAsset(name: "Map Icons/AccessDenied")
}
}
}
I'd love for the generated enums to implement CaseIterable, since that would allow people to list all available values. Is this supported in some way?
I now manually add it manually after each generation.
internal enum Asset {
internal enum Colors {
internal static let skyBlue = ColorAsset(name: "skyBlue")
}
internal enum Images {
internal enum MapIcons: CaseIterable {
internal static let accessDenied = ImageAsset(name: "Map Icons/AccessDenied")
}
}
}
Is this something that SwiftGen could support?
CaseIterable won't do anything though, as there are no enum cases, see my 1st comment in that issue:
The thing with
CaseIterableis that there need to be actual cases in theenums. In our (SwiftGen) case, the enums are empty and only used for namespacing purposes, note that we only have static constants.
Yeah, CaseIterable probably isn't the tool then, but you could generate an allImages property that just includes all generated images within a namespace. This would be immensely helpful.
You can pass allValues parameter to the template: https://github.com/SwiftGen/SwiftGen/blob/2762ded15bd72fa9f3d6f16bc19530c10a418d99/Documentation/templates/xcassets/swift4.md
There we go, thank you!
@ikesyo How do you provide the template with allValues? I've tried:
xcassets:
allValues:
inputs:
- Pinny/Assets/Colors.xcassets
- Pinny/Assets/Images.xcassets
outputs:
- templateName: swift4
output: Pinny/Assets/Assets.swift
and
xcassets:
inputs:
- Pinny/Assets/Colors.xcassets
- Pinny/Assets/Images.xcassets
outputs:
- templateName: swift4
allValues:
output: Pinny/Assets/Assets.swift
but none of this works.
It should be:
xcassets:
inputs:
- Pinny/Assets/Colors.xcassets
- Pinny/Assets/Images.xcassets
outputs:
- templateName: swift4
output: Pinny/Assets/Assets.swift
params:
allValues: true
Check our config documentation: https://github.com/SwiftGen/SwiftGen/blob/master/Documentation/ConfigFile.md
Thanks! I checked the documentation, but didn't see an example of how to provide custom params. I tried your suggestion and it worked perfectly!
hey @danielsaidi ,
I tried to use allValues: true in my yml file. But it still doesn't create allValues with list of all values. I am missing any thing?
I am using swiftgen v6.4.0 thanks.
outputs:
templateName: structured-swift5
output: MyCopy.swift
params:
allValues: true
cc: @djbe
@mysticvalley Sorry, but I don鈥檛 use allValues in my current apps atm 馃槙
Most helpful comment
You can pass
allValuesparameter to the template: https://github.com/SwiftGen/SwiftGen/blob/2762ded15bd72fa9f3d6f16bc19530c10a418d99/Documentation/templates/xcassets/swift4.md