I have a SwiftUI View with two similar init blocks. The only thing different between them is the type of an Optional property (has a default value). Because of the fact that it is optional, when I call the view without it, I get an Ambiguous use of 'init' error.
I want the first init to be the default initializer for the View.
Here's what it looks like:
struct NewView: View {
var a: String
var b: String
var c: AnyShapeStyle
var d: String
init(a: String,
b: String,
c: LinearGradient = LinearGradient(gradient: Gradient(colors: [Color.green, Color.blue]), startPoint: .top, endPoint: .bottom),
d: String = "Linear") {
self.a = a
self.b = a
self.c = AnyShapeStyle(c)
self.d = d
}
init(a: String,
b: String,
c: AngularGradient = AngularGradient(colors: [.green, .blue], center: .center),
d: String = "Angular") {
self.a = a
self.b = a
self.c = AnyShapeStyle(c)
self.d = d
}
...
}
Hence calling the view like this would give me the error:
NewView(a: "a", b: "b")
AngularGradient(colors: [.green, .blue], center: .center)to be used?