【发布时间】:2020-12-14 02:12:12
【问题描述】:
似乎无法正确更改哈希(进入:)?我尝试的一切都带来了一个新的错误
对此的任何帮助将不胜感激, 斯威夫特 5
import UIKit
import TTFortuneWheel
class CarnivalWheel: FortuneWheelSliceProtocol, Codable, Hashable {
static func == (lhs: CarnivalWheel, rhs: CarnivalWheel) -> Bool {
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
}
var hashValue: Int {
return ObjectIdentifier(self).hashValue
}
enum Style: String, Codable, Hashable {
case blue
case purple
case green
case grey
case orange
case yellow
}
var style: Style = .blue
var backgroundColor: UIColor? {
switch style {
case .blue: return TTUtils.uiColor(from: 0xdff9fb)
case .purple: return TTUtils.uiColor(from: 0xa29bfe)
case .green: return TTUtils.uiColor(from: 0x7bed9f)
case . grey: return TTUtils.uiColor(from: 0xdfe4ea)
case .orange: return TTUtils.uiColor(from: 0xffbe76)
case .yellow: return TTUtils.uiColor(from: 0xf6e58d)
}
}
var title: String
var degree: CGFloat = 0.0
init(title: String) {
self.title = title
}
var fontColor: UIColor {
return UIColor.black
}
var offsetFromExterior: CGFloat {
return 10.0
}
var stroke: StrokeInfo? {
return StrokeInfo(color: UIColor.white, width: 1.0)
}
convenience init(title: String, degree: CGFloat) {
self.init(title: title)
self.degree = degree
}
}
【问题讨论】:
-
请阅读documentation,因为不清楚
ObjectIdentifier是什么 -
@vadian,可能是这个:developer.apple.com/documentation/swift/objectidentifier
-
@Jamesnjones,您可以删除您的自定义
hashValue变量,Hashable可能会为您生成它。所有CarnivalWheel属性看起来都像Hashable,所以这是可能的。 -
如果我删除了我的自定义变量,它会报错说嘉年华轮不符合 Hashable 并且因为它的类自动合成 'Hashable' 不受支持
-
@Jamesnjones,您提供的代码是否完整?因为我只看到
Hashable类型Style的style类型Hashable类型String的title和degree的3 个存储属性Hashable“类型”CGFloat。Hashable的自动实现应该是可能的。