ios - How to Add custom init in SwiftUI View extension - Stack Overflow

Let's sayI have my enum with spacings: 5case tiny = 5.0}and I want to create init for VStack

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?

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?

Share asked Nov 22, 2024 at 7:35 GorthezGorthez 4211 gold badge6 silver badges14 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 3

A completely different approach to this problem is to use an enum with static properties, that way you can still use the enum (without having to access rawValue) when creating your VStack and you don't need to implement an extra init

enum SpacingsUI {
    static let tiny: CGFloat = 5.0
    //
}
VStack(spacing: SpacingsUI.tiny) {

}

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)
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742291613a4416293.html

相关推荐

  • ios - How to Add custom init in SwiftUI View extension - Stack Overflow

    Let's sayI have my enum with spacings: 5case tiny = 5.0}and I want to create init for VStack

    19小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信