- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我的 iOS 项目一直面临以下问题(这只是一个警告)。
'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'ActiveType' to 'Hashable' by implementing 'hash(into:)' instead
源代码:
public enum ActiveType {
case mention
case hashtag
case url
case custom(pattern: String)
var pattern: String {
switch self {
case .mention: return RegexParser.mentionPattern
case .hashtag: return RegexParser.hashtagPattern
case .url: return RegexParser.urlPattern
case .custom(let regex): return regex
}
}
}
extension ActiveType: Hashable, Equatable {
public var hashValue: Int {
switch self {
case .mention: return -1
case .hashtag: return -2
case .url: return -3
case .custom(let regex): return regex.hashValue
}
}
}
有更好的解决方案吗?警告本身建议我实现“hash(into:)”,但我不知道如何实现?
引用:ActiveLabel
最佳答案
正如警告所说,现在您应该改为实现 hash(into:)
函数。
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
}
}
移除自定义hash(into:)
实现(除非您需要特定的实现)会更好(在枚举和结构的情况下),因为编译器会自动为您合成它.
只需让您的枚举符合它:
public enum ActiveType: Hashable {
case mention
case hashtag
case url
case custom(pattern: String)
var pattern: String {
switch self {
case .mention: return RegexParser.mentionPattern
case .hashtag: return RegexParser.hashtagPattern
case .url: return RegexParser.urlPattern
case .custom(let regex): return regex
}
}
}
关于Swift: 'Hashable.hashValue' 作为协议(protocol)要求已弃用;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55395207/
有没有人写过一个通用函数以便hash可以为自定义数据类型自动生成函数(使用 deriving 机制)?有几次,我写了以下样板, data LeafExpr = Var Name | Star deri
我们如何让 SwiftUI 对象,尤其是 Image,符合 Hashable 协议(protocol)? 我知道它们符合 Equatable 协议(protocol),所以主要问题是如何获取哈希值,或
我需要某种薄包装对象来标记字典键,例如: d = { Required('name'): str, Optional('age'): int, } 并且这些包装器的行为应该像被包装的对象(比较
我实现了下面的类: class Table : Hashable { var uid : Int var timeRemaining : Int? var curren
有时我编写的函数的参数可以是任何类型,只要它是 hashable。 - 例如,因为我的函数将其添加到集合中或将其用作字典键。 有没有一种方法可以使用 Python 3.5 中引入的 PEP 484 类
这个问题已经有答案了: Make struct Hashable? (2 个回答) 已关闭 5 年前。 我希望创建一个仅保存结构的唯一实例的数组: var vowelSet: Set 这是我的结构
protocol Component {} struct Container { let map: [Component: Component] } 在上面的代码中,如何指定 map 的键可以
我有一个自定义控件,它使用数据源来获取项目(就像 NSTableView 所做的那样)。数据源可以返回任何类型,只要它是可哈希的。这些项目用作私有(private)字典中的键。 控件(自定义 View
在 Hashable 内部我们可以看到: /// Hash values are not guaranteed to be equal across different executions of /
在 Xcode 10 和 Swift 4.2 中,还有其他类型符合 Hashable,只要它们的元素也符合 Hashable(Array 、字典等)。 我目前在我的项目中有一些代码为 Swift 4.
我想使用自定义枚举数组作为字典键,但我很难弄清楚如何使数组符合 Hashable。编译器告诉我 [Symbol] 不符合 Hashable。我需要做什么才能编译它? 我搞砸了对 Array 的扩展,其
假设我有一个名为 LivingCreature 的类以及继承自它的其他类: 人类 狗 外星人 这就是我想要完成的: let valueForLivingCreature = Dictionary 然后
我有一些结构符合的基本协议(protocol)(模型)。它们也符合 Hashable protocol Model {} struct Contact: Model, Hashable { v
考虑以下代码: #!/usr/bin/env python3.7 from typing import NamedTuple, Set class Person(NamedTuple): na
我有一个自定义的映射类,如下所示。 class UserSaved { var Id : String var UserName : String var
它在 java 中很容易完成 - 哈希码似乎是指向对象或其他东西的指针。为什么 swift 没有为我们提供同样的舒适感,而是要求我们自己定义函数? 最佳答案 哈希码似乎是指向对象或其他东西的指针。 在
我正在尝试在 Swift 中创建一个函数,它将一个字符串字典作为参数,并返回一个字符串元组。我希望字典中的键值对是可选的,因为如果它返回的元组中的值之一是“nil”,我不希望我的程序崩溃。 func
我正在尝试修复 RayWenderlich 网站上不再受支持的旧教程。该警告出现在三个文件中,Chain.swift、Cookie.swift 和 Swap.swift,来自“如何使用 SpriteK
如何在隐藏实现 Hashable 的具体类型的地方返回 [Hashable: Any]? Swift 有两种类型的协议(protocol):可以用作类型的协议(protocol)和只能用作类型约束的协
假设我有一个基类“Person”,我想将其添加到一个集合(列表)中,因此需要符合 Hashable 和 Equatable: class Person : Equatable, Hashable {
我是一名优秀的程序员,十分优秀!