-2

Cant seem to get the hash(into:) changed correctly? everything i try just brings a new error

Any help on this would be appreciated, Swift 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
    }
}
user28434'mstep
  • 6,290
  • 2
  • 20
  • 35
  • Please read the [documentation](https://developer.apple.com/documentation/swift/hashable) because it's unclear what `ObjectIdentifier` is – vadian Aug 25 '20 at 11:39
  • @vadian, probably this: https://developer.apple.com/documentation/swift/objectidentifier – user28434'mstep Aug 25 '20 at 11:43
  • @Jamesnjones, you can just delete your custom `hashValue` variable, and `Hashable` may generate it for you. All `CarnivalWheel` properties looks like `Hashable`, so it would be possible. – user28434'mstep Aug 25 '20 at 11:45
  • if i remove my custom variable it then errors saying Carnival wheel doesn't conform to Hashable and because its a class automatic synthesis of 'Hashable' is not supported – Jamesnjones Aug 25 '20 at 11:55
  • @Jamesnjones, *Is your presented code complete*? Because I see only 3 stored properties `style` of `Hashable` type `Style`, `title` of `Hashable` type `String` and `degree` of `Hashable` "type" `CGFloat`. Auto-implementation of `Hashable` should be possible. – user28434'mstep Aug 26 '20 at 09:04
  • Does this answer your question? [Swift: 'Hashable.hashValue' is deprecated as a protocol requirement;](https://stackoverflow.com/questions/55395207/swift-hashable-hashvalue-is-deprecated-as-a-protocol-requirement) – Max MacLeod Dec 23 '20 at 11:57

1 Answers1

1
func hash(into hasher: inout Hasher) {
        hasher.combine(ObjectIdentifier(self))
    }

will add conformance.

cazman
  • 1,452
  • 1
  • 4
  • 11
  • `looks to create the same hash value as you have above` — nope. `Hasher`'s hash won't even be the same each time you run your app. – user28434'mstep Aug 26 '20 at 08:59