outfoxx / swiftpoet

Kotlin and Java API for generating .swift source files.
Apache License 2.0
277 stars 26 forks source link

Support `convenience init` #83

Closed timrijckaert closed 6 months ago

timrijckaert commented 1 year ago

Hello I was wondering how following Swift code could be generated:

extension UIColor {
    public convenience init(hex: Int, alpha: CGFloat = 1) {
        let components = (
            r: CGFloat((hex >> 16) & 0xFF) / 255,
            g: CGFloat((hex >> 08) & 0xFF) / 255,
            b: CGFloat((hex >> 00) & 0xFF) / 255
        )
        self.init(red: components.r, green: components.g, blue: components.b, alpha: alpha)
    }
}

I tried following

.addExtension(
    ExtensionSpec.builder(uiColorType)
        .addFunction(
            FunctionSpec.constructorBuilder()
                .addModifiers(Modifier.PUBLIC)
                .build()
        )
        .build()
)

Which yields the init function but no option to add the convenience keyword.

extension UIColor {
  public init() {
  }
}