0

Hasher.combine fails with compile error: Error:(31, 24) Missing argument label 'bytes:' in call when I try to pass a string. Here is the code:

private class AssetCacheKey : NSObject {
    let id: String
    let size: CGSize

    init(id: String, size: CGSize) {
        self.id = id
        self.size = size
    }

    private func isEqual(_ other: AssetCacheKey) -> Bool {
        return other.id == id && other.size == size
    }

    override func isEqual(_ object: Any?) -> Bool {
        return (object as? AssetCacheKey)?.isEqual(self) ?? false
    }

    override class func hash() -> Int {
        var hasher = Hasher()
        hasher.combine(id)
        //hasher.combine(size)
        return hasher.finalize()
    }
}
Semyon
  • 3,540
  • 5
  • 34
  • 57
  • 1
    Could it be that the actual problem is with `size` and not with the string `id`? `CGSize` does not conform to the Hashable protocol. – Martin R Oct 02 '19 at 09:53
  • 1
    Another problem is that you made `func hash()` a *class method.* What are you trying to achieve? – Martin R Oct 02 '19 at 09:55
  • As you see in the code, it's commented. But still fires an error: //hasher.combine(size) – Semyon Oct 02 '19 at 09:57
  • NSObject conforms to Hashable so I think you should override hashValue instead? – Joakim Danielson Oct 02 '19 at 09:59
  • @SemyonTikhonenko: That is because you made it a *class* method. – Note also that for NSObject subclasses you must override `var hash : Int`, compare https://stackoverflow.com/q/33319959. – Martin R Oct 02 '19 at 10:00
  • @JoakimDanielson: No, for NSObject subclasses you must override `hash` and `isEqual:` – Martin R Oct 02 '19 at 10:01
  • func hash() a class method was a problem. Not only this one, but this one causes the compiler error. @MartinR, thanks – Semyon Oct 02 '19 at 10:14

0 Answers0