存储的 属性 类型 'CGPoint' 不符合协议 'Hashable',
Stored property type 'CGPoint' does not conform to protocol 'Hashable',
想象一下纸板。所有卡片都在我称为 CardBoard 的视图上。
所有卡片都在一个数组上:
var cards:[Card]
每张牌都有自己的位置。
这是卡片
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)
}
}
现在我想把它们画在屏幕上。
var body: some View {
ZStack {
ForEach(cards, id:\.self) {card in
}
}
}
当我尝试这个时,Xcode 告诉我
Generic struct 'ForEach' requires that 'Card' conform to 'Hashable'
当我将 Hashable
添加到 Card
struct Card: View, Hashable {
1. Stored property type 'CGPoint' does not conform to protocol 'Hashable', preventing synthesized conformance of 'Card' to 'Hashable'
有什么想法吗?
问题是您的 Card
的 point
无法自动合成,因为它是 CGPoint
。您是否尝试过扩展 CGPoint
以符合 Hashable
?
这里有一个关于为 CGPoint
创建散列逻辑的非常有用的讨论:https://codereview.stackexchange.com/questions/148763/extending-cgpoint-to-conform-to-hashable
谢谢大家,但是因为 post 不适合 link 作为解决方案,我 post 在这里:
解决方案是使视图可散列并添加:
extension CGPoint : Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(x)
hasher.combine(y)
}
}
有很多类型需要相同的处理(通常称为二元组)。例如
extension CGPoint: HashableSynthesizable { }
extension CLLocationCoordinate2D: HashableSynthesizable { }
extension MKCoordinateRegion: HashableSynthesizable { }
extension MKCoordinateSpan: HashableSynthesizable { }
/// A type whose `Hashable` conformance could be auto-synthesized,
/// but either the API provider forgot, or more likely,
/// the API is written in Objective-C, and hasn't been modernized.
public protocol HashableSynthesizable: Hashable { }
public extension HashableSynthesizable {
static func == (hashable0: Self, hashable1: Self) -> Bool {
zip(hashable0.hashables, hashable1.hashables).allSatisfy(==)
}
func hash(into hasher: inout Hasher) {
hashables.forEach { hasher.combine([=11=]) }
}
}
private extension HashableSynthesizable {
var hashables: [AnyHashable] {
Mirror(reflecting: self).children
.compactMap { [=11=].value as? AnyHashable }
}
}
想象一下纸板。所有卡片都在我称为 CardBoard 的视图上。
所有卡片都在一个数组上:
var cards:[Card]
每张牌都有自己的位置。
这是卡片
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)
}
}
现在我想把它们画在屏幕上。
var body: some View {
ZStack {
ForEach(cards, id:\.self) {card in
}
}
}
当我尝试这个时,Xcode 告诉我
Generic struct 'ForEach' requires that 'Card' conform to 'Hashable'
当我将 Hashable
添加到 Card
struct Card: View, Hashable {
1. Stored property type 'CGPoint' does not conform to protocol 'Hashable', preventing synthesized conformance of 'Card' to 'Hashable'
有什么想法吗?
问题是您的 Card
的 point
无法自动合成,因为它是 CGPoint
。您是否尝试过扩展 CGPoint
以符合 Hashable
?
这里有一个关于为 CGPoint
创建散列逻辑的非常有用的讨论:https://codereview.stackexchange.com/questions/148763/extending-cgpoint-to-conform-to-hashable
谢谢大家,但是因为 post 不适合 link 作为解决方案,我 post 在这里:
解决方案是使视图可散列并添加:
extension CGPoint : Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(x)
hasher.combine(y)
}
}
有很多类型需要相同的处理(通常称为二元组)。例如
extension CGPoint: HashableSynthesizable { }
extension CLLocationCoordinate2D: HashableSynthesizable { }
extension MKCoordinateRegion: HashableSynthesizable { }
extension MKCoordinateSpan: HashableSynthesizable { }
/// A type whose `Hashable` conformance could be auto-synthesized,
/// but either the API provider forgot, or more likely,
/// the API is written in Objective-C, and hasn't been modernized.
public protocol HashableSynthesizable: Hashable { }
public extension HashableSynthesizable {
static func == (hashable0: Self, hashable1: Self) -> Bool {
zip(hashable0.hashables, hashable1.hashables).allSatisfy(==)
}
func hash(into hasher: inout Hasher) {
hashables.forEach { hasher.combine([=11=]) }
}
}
private extension HashableSynthesizable {
var hashables: [AnyHashable] {
Mirror(reflecting: self).children
.compactMap { [=11=].value as? AnyHashable }
}
}