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?
2
Answers
Because
VStack
already have a initinit(alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil, @ViewBuilder content: () -> Content)
, so when you try overload it withConst.SpacingSUI
and default valuenil
and callVStack.init(content: {
which implicit indicatespacing
isnil
, compiler can not know which function it should call. A solution could be remove default value from your custom initA 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