Swiftgen: Asset catalog name collisions

Created on 6 Apr 2018  路  2Comments  路  Source: SwiftGen/SwiftGen

I've organized my asset catalog using folders, which SwiftGen nicely uses as enums/namespaces. But, I've noticed it loads the incorrect asset if I use the same name for multiple assets.

For example, I have two "add" image assets. This works well because they are in different folders in the asset catalog:
View1/Icons/add
View2/Icons/add

Usage with SwiftGen is something like:
Asset.View2.Icons.add, but since the filenames conflict ("add") this actually will return the Asset for View1.

I looked around the code a little and noticed this is because the ImageAsset is a wrapper around the UIImage(named: )/NSImage(named: ) initializers, which only takes the name (and doesn't care about Folders in the asset catalog).

Working around it is pretty easy (just use longer/unique names), but then organizing assets into folders is kind of pointless. Is there a different workaround?

question

Most helpful comment

Weird that Xcode doesn't give you an error (or warning) for this.

Anyway, this is where the concept of "provides namespace" comes in. Each of those folders in your catalog have a small checkbox with this property, that'll ensure that the name of your images will actually be "Views1/Icons/add" and "Views2/Icons/add", avoiding any naming collisions.

All 2 comments

Weird that Xcode doesn't give you an error (or warning) for this.

Anyway, this is where the concept of "provides namespace" comes in. Each of those folders in your catalog have a small checkbox with this property, that'll ensure that the name of your images will actually be "Views1/Icons/add" and "Views2/Icons/add", avoiding any naming collisions.

@djbe Thanks! I didn't know about that setting! 馃帀

I've been using AppCode a bit so that's how I missed the warning, when I loaded the project in Xcode I saw it.

Was this page helpful?
0 / 5 - 0 ratings