Reputation: 4450
When I use a custom view in a ForEach
I get Type '_' has no member 'id' error. When I use Text(item.x)
instead of the custom view it compiles and works. What am I missing?
@State private var showTargets = [
(id: 1, state: false, x: 109.28, y: 109.28),
(id: 2, state: false, x: 683, y: 109.28),
(id: 3, state: false, x: 1256.72, y: 109.28)
]
...
var body: some View {
Group {
ForEach(showTargets, id: \.id) { item in
Text(String(item.x))
// Using CustomView(x: item.x, y: item.y, f: {}) instead does not work
}
}
Custom View:
struct CustomView : View {
@State private var color = Color.white
@State private var animate = false
internal var x: CGFloat
internal var y: CGFloat
internal var f: ()->()
internal let w: CGFloat = 60
internal let h: CGFloat = 60
private let width = -1366/2
private let height = -1024/2
var body: some View {
Button(action: {
self.animate.toggle()
if self.color == Color.green {
self.color = Color.white
}
else {
self.color = Color.green
self.f()
}
}, label: {
Ellipse()
.fill(self.color)
.scaleEffect(self.animate ? 1.2 : 1)
.animation(Animation.easeInOut)
}).position(x: self.x + self.w/2, y: self.y + self.h/2)
.frame(width: self.w, height: self.h, alignment: .center)
.offset(CGSize(width: self.width, height: self.height))
}
}
Upvotes: 4
Views: 2926
Reputation: 11066
You are trying to initialize your CustomView
with the wrong types (Double
instead of CGFloat
).
Your CustomView
initializer looks like this:
init(x: CGFloat, y: CGFloat, f: () -> Void)
And you call it using the showTargets
tuple values, which is:
(id: Int, state: Bool, x: Double, y: Double)
So when you do this:
CustomView(x: item.x, y: item.y, f: {})
You are providing Double
values (for x and y) instead of CGFloat
. As the cast from Double
to CGFloat
cannot be implicit, you need to do it explicitly:
CustomView(x: CGFloat(item.x), y: CGFloat(item.y), f: {})
Upvotes: 4