SwiftUI ForEach 类型“_”没有成员'id'
SwiftUI ForEach Type '_' has no member 'id'
当我在 ForEach
中使用自定义视图时,出现类型“_”没有成员 'id' 错误。当我使用 Text(item.x)
而不是自定义视图时,它会编译并运行。我错过了什么?
@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
}
}
自定义视图:
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))
}
}
您正在尝试使用错误的类型(Double
而不是 CGFloat
)初始化 CustomView
。
您的 CustomView
初始值设定项如下所示:
init(x: CGFloat, y: CGFloat, f: () -> Void)
并且您使用 showTargets
元组值调用它,即:
(id: Int, state: Bool, x: Double, y: Double)
所以当你这样做时:
CustomView(x: item.x, y: item.y, f: {})
您正在提供 Double
个值(对于 x 和 y)而不是 CGFloat
。由于从 Double
到 CGFloat
的转换不能是隐式的,您需要明确地进行转换:
CustomView(x: CGFloat(item.x), y: CGFloat(item.y), f: {})
当我在 ForEach
中使用自定义视图时,出现类型“_”没有成员 'id' 错误。当我使用 Text(item.x)
而不是自定义视图时,它会编译并运行。我错过了什么?
@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
}
}
自定义视图:
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))
}
}
您正在尝试使用错误的类型(Double
而不是 CGFloat
)初始化 CustomView
。
您的 CustomView
初始值设定项如下所示:
init(x: CGFloat, y: CGFloat, f: () -> Void)
并且您使用 showTargets
元组值调用它,即:
(id: Int, state: Bool, x: Double, y: Double)
所以当你这样做时:
CustomView(x: item.x, y: item.y, f: {})
您正在提供 Double
个值(对于 x 和 y)而不是 CGFloat
。由于从 Double
到 CGFloat
的转换不能是隐式的,您需要明确地进行转换:
CustomView(x: CGFloat(item.x), y: CGFloat(item.y), f: {})