iosswiftswiftui

How to Add custom init in SwiftUI View extension


Let's say I have my enum with spacings:

        /// 5
        case tiny = 5.0
    }

and I want to create init for VStack with spacing: Const.SpacingSUI, sth like this:

extension VStack {
    init(alignment: HorizontalAlignment = .center, spacing: Const.SpacingSUI? = nil, @ViewBuilder content: () -> Content) {
        self.init(alignment: alignment, spacing: spacing?.rawValue, content: content)
    }
}

and I'm getting error in compile time: Ambiguous use of 'init(alignment:spacing:content:)' Ok, I get it. If I use all the parameters, it will be fine; otherwise, there will be an error. But is it possible to have additional init with my custom type in spacing parameter?


Solution

  • Because VStack already have a init init(alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil, @ViewBuilder content: () -> Content), so when you try overload it with Const.SpacingSUI and default value nil and call VStack.init(content: { which implicit indicate spacing is nil, compiler can not know which function it should call. A solution could be remove default value from your custom init

    extension VStack {
        init(alignment: HorizontalAlignment = .center, spacing: Const.SpacingSUI, @ViewBuilder content: () -> Content) {
            self.init(alignment: alignment, spacing: spacing.rawValue, content: content)
        }
    }