-1

I'm using swiftgen to generate some enums for my iOS app but the strings that we use have words in them like... "something_string_something" or "something_float_something".

This means that when the enums are generated it creates...

public enum String {
...
}

public enum Float {
...
}

And then the corresponding functions that use variables like...

public enum Something {
  public static func name(_ p1: Int) -> String {}
}

Then have a compile error because the compiler thinks that the String in the return type is actually the public enum String and not just standard Swift String.

Is it possible to define alternative keys in this case? Like if I wanted it to out using public enum Strng and public enum Flt instead to avoid the collision?

Obviously, if I could change the originating strings I would but they are not so easy to change in a mature project like the one I'm working on.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • 1
    I'd go with a custom parser maybe: https://github.com/SwiftGen/SwiftGen/blob/stable/Documentation/Parsers/strings.md Maybe explicit the expected type, so it's using `Something.String` instead of `Swift.String`. Maybe there is a template for that. – Larme Nov 26 '21 at 09:21
  • @Larme Ah, I didn't know it was possible to use a custom parser. Thanks, I'll take a look at that. – Fogmeister Nov 26 '21 at 09:31

1 Answers1

0

Following @Larme's advice I created my own custom template using this documentation... https://github.com/SwiftGen/SwiftGen/blob/stable/Documentation/Articles/Creating-custom-templates.md

I copied the pre-made template I wanted to use and then changed it in a few places to add Swift. to several of the types.

So now instead of using String, Int, Float, etc... it uses Swift.String, Swift.Int, Swift.Float and generates and builds with no issues.

Thanks @Larme

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • 1
    Maybe it's even worth a PR on SwiftGen to have its template use Swift prefix since it wouldn't hurt to ensure that the returned type is indeed `Swift.String` and some values are `Swift.Int` etc when there are placeholders. – Larme Nov 26 '21 at 11:02