I've still (very slowly) been working on color arrays in SwiftUI, @roosterboy was incredibly helfpul here and yet I've been wanting to explore other ways to do it and tried ChatGPT for the first time today.
I stated: "In SwiftUI create an array of 3 rectangles with fills of distinct colors created in equal steps by blending two different parent colors"
And got code that looked promising in it's simplicity but did not work:
let parentColors = [Color.red, Color.blue]
var body: some View {
VStack {
ForEach(0..<3, id: \.self) { index in
Rectangle()
.fill(LinearGradient(
gradient: Gradient(colors: [
parentColors[0].opacity(1 - CGFloat(index) / 3),
parentColors[1].opacity(1 - CGFloat(index) / 3)
]),
startPoint: .leading,
endPoint: .trailing
))
}
}
}
The error was Ambiguous use of 'init(_:)'
I then asked ChatGPT to try to fix for that error and it explained that:
The error message "Ambiguous use of 'init(_:)'" indicates that there is more than one initializer that matches the arguments you have provided. This means that the compiler is unable to determine which initializer you are trying to use.
In the code you provided, it seems that the error is occurring when you are trying to create a LinearGradient with the Gradient initializer. This is because the Gradient initializer has multiple signatures that take different arguments, and the compiler is unable to determine which one you are trying to use based on the arguments you have provided.
To fix this error, you can specify the type of gradient you want to create by using the appropriate initializer for that gradient type. For example, if you want to create a LinearGradient, you can use the initializer that takes a Gradient and two CGPoint values for the start and end points of the gradient:
But from there it just gave the same code.
I tried learn about initializing Gradient etc. to no avail. I'm curious if you think ChatGPT is onto something or if SwiftUI is just not set up to create steps of color arrays without getting into something like Extension of Color... like we did in the link I shared at the top.