Spa*_*Dog 3 hashable swift swiftui
想象一下卡片板。所有卡片都位于我称之为 CardBoard 的视图中。
所有卡都在一个数组中:
var cards:[Card]
Run Code Online (Sandbox Code Playgroud)
每张牌都有自己的位置。
这是卡
struct Card: View {
let id = UUID()
var name:String
var code:String
var filename:String
var position = CGPoint(x: 200, y: 250)
init(name:String, code:String, filename:String) {
self.name = name
self.code = code
self.filename = filename
}
var body: some View {
Image(filename)
.position(position)
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想把它们画在屏幕上。
var body: some View {
ZStack {
ForEach(cards, id:\.self) {card in
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,Xcode 告诉我
Generic struct 'ForEach' requires that 'Card' conform to 'Hashable'
Run Code Online (Sandbox Code Playgroud)
当我添加Hashable
到Card
struct Card: View, Hashable {
1. Stored property type 'CGPoint' does not conform to protocol 'Hashable', preventing synthesized conformance of 'Card' to 'Hashable'
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
Spa*_*Dog 10
谢谢大家,但是因为发布链接作为解决方案并不好,所以我在这里发布:
解决方案是使视图可哈希并添加以下内容:
extension CGPoint : Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(x)
hasher.combine(y)
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1710 次 |
最近记录: |