【问题标题】:'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'CarnivalWheel' to 'Hashable' by implementing 'hash(into:)' instead'Hashable.hashValue' 作为协议要求已被弃用;通过实现 'hash(into:)' 来使类型 'CarnivalWheel' 符合 'Hashable'
【发布时间】: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 是什么
  • @Jamesnjones,您可以删除您的自定义 hashValue 变量,Hashable 可能会为您生成它。所有CarnivalWheel 属性看起来都像Hashable,所以这是可能的。
  • 如果我删除了我的自定义变量,它会报错说嘉年华轮不符合 Hashable 并且因为它的类自动合成 'Hashable' 不受支持
  • @Jamesnjones,您提供的代码是否完整?因为我只看到Hashable 类型Stylestyle 类型Hashable 类型Stringtitledegree 的3 个存储属性Hashable“类型”CGFloatHashable 的自动实现应该是可能的。

标签: ios swift class hashable


【解决方案1】:
func hash(into hasher: inout Hasher) {
        hasher.combine(ObjectIdentifier(self))
    }

将增加一致性。

【讨论】:

  • looks to create the same hash value as you have above — 不。每次运行应用时,Hasher 的哈希甚至都不相同。
猜你喜欢
  • 2019-08-19
  • 2019-12-11
  • 1970-01-01
  • 1970-01-01
  • 2017-07-11
  • 2021-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多