21

コンパイラからの非推奨警告を使用hashValueせずに実装する方法については、まったくわかりませんhash(into:)

「Hashable.hashValue」はプロトコル要件として非推奨です。代わりに「hash(into:)」を実装することにより、タイプ「MenuItem」を「Hashable」に適合させます

Swiftからの回答: 'Hashable.hashValue' はプロトコル要件として非推奨です。この例があります:

func hash(into hasher: inout Hasher) {
    switch self {
    case .mention: hasher.combine(-1)
    case .hashtag: hasher.combine(-2)
    case .url: hasher.combine(-3)
    case .custom(let regex): hasher.combine(regex) // assuming regex is a string, that already conforms to hashable
    }
}

PagingItemそして、Parchment ( https://github.com/rechsteiner/Parchment )をカスタマイズするために、この構造体を持っています。

import Foundation

/// The PagingItem for Menus.
struct MenuItem: PagingItem, Hashable, Comparable {
    let index: Int
    let title: String
    let menus: Menus

    var hashValue: Int {
        return index.hashValue &+ title.hashValue
    }

    func hash(into hasher: inout Hasher) {
        // Help here?
    }

    static func ==(lhs: MenuItem, rhs: MenuItem) -> Bool {
        return lhs.index == rhs.index && lhs.title == rhs.title
    }

    static func <(lhs: MenuItem, rhs: MenuItem) -> Bool {
        return lhs.index < rhs.index
    }
}
4

2 に答える 2