Enhancement: Assets as extensions to system types #901
Replies: 1 comment 2 replies
-
A problem with this proposal is that your assets can conflict with apples own built in assets. swiftgen already generates its own initializers for most assets. For example:
These are also not very convenient to use because they are optional. They are also more verbose than they need to be because you have to reference the main asset enum. I propose that some new extensions are generated to solve this. Using Colors as an example: Firstly, static ColorAssets should also be generated inside of the ColorAsset class. extension ColorAsset {
static let red = ColorAsset(name: "red")
static let green = ColorAsset(name: "green")
static let blue = ColorAsset(name: "blue")
} Secondly a new function should be added to Color typealias: extension ColorAsset.Color {
static func color(_ colorAsset: ColorAsset) -> Color {
colorAsset.color
}
} The new usage would be: view.backgroundColor = .color(.red) Not as convenient as your proposal, but its less verbose than what we have today, and won't conflict with the built in colors/images. |
Beta Was this translation helpful? Give feedback.
-
I would like to be able to pass an optional parameter to the SwiftGen configuration so that assets are added as extensions to system types instead of / next to the usual SwiftGen structures.
Examples:
Colors
Color assets would be extensions to UIColor/NSColor:
Images
Image assets would be extensions to UIImage/NSImage:
Strings:
Please let me know if my idea is valid and useful (by voting on the issue). This mechanism could also be applied to Fonts, maybe Storyboards. If time allows maybe I will try and implement it myself.
Beta Was this translation helpful? Give feedback.
All reactions