- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
Swift 中的Hashable
协议(protocol)要求您实现一个名为hashValue
的属性:
protocol Hashable : Equatable {
/// Returns the hash value. The hash value is not guaranteed to be stable
/// across different invocations of the same program. Do not persist the hash
/// value across program runs.
///
/// The value of `hashValue` property must be consistent with the equality
/// comparison: if two values compare equal, they must have equal hash
/// values.
var hashValue: Int { get }
}
不过,似乎还有一个类似的属性,叫做hash
。
hash
和 hashValue
有什么区别?
最佳答案
hash
是 NSObject
protocol 中的必需属性,它对所有 Objective-C 对象的基础方法进行了分组,因此早于 Swift。默认实现只返回对象地址,正如你所看到的 NSObject.mm , 但可以覆盖该属性在 NSObject
子类中。
hashValue
是 Swift Hashable
协议(protocol)的必需属性。
两者都通过 NSObject
扩展连接Swift 中的标准库 ObjectiveC.swift :
extension NSObject : Equatable, Hashable {
/// The hash value.
///
/// **Axiom:** `x == y` implies `x.hashValue == y.hashValue`
///
/// - Note: the hash value is not guaranteed to be stable across
/// different invocations of the same program. Do not persist the
/// hash value across program runs.
open var hashValue: Int {
return hash
}
}
public func == (lhs: NSObject, rhs: NSObject) -> Bool {
return lhs.isEqual(rhs)
}
(关于open var
的含义,参见What is the 'open' keyword in Swift?。)
因此 NSObject
(以及所有子类)符合 Hashable
协议(protocol),以及默认的 hashValue
实现返回对象的 hash
属性。
isEqual
方法之间存在类似的关系NSObject
协议(protocol),以及 Equatable
中的 ==
运算符协议(protocol):NSObject
(和所有子类)符合Equatable
协议(protocol),以及默认的 ==
实现在操作数上调用 isEqual:
方法。
关于swift - Swift 的 hash 和 hashValue 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38946690/
我们发现,如果一个是另一个的负数,则无法通过哈希值来区分两个小数。我们将 Decimals 用作结构中的字段,并且该结构实现了 Hashable 以便能够放入集合中。我们的业务逻辑然后要求所有字段都是
这是 Swift 1.2,我使用的是 Xcode 6.4。以下枚举具有可失败的初始化程序。 enum EstimateItemStatus: Int, Printable { case Pen
由于某种原因,在实现用于其他结构的对象 hashValue 时,程序崩溃了。当我试图获取任何字符串对象的 hashCode 时,它似乎崩溃了。它正在扩展一个结构,但在使用 hashValue 时,
我正在尝试修复 RayWenderlich 网站上不再受支持的旧教程。该警告出现在三个文件中,Chain.swift、Cookie.swift 和 Swap.swift,来自“如何使用 SpriteK
hastable 在对象上使用一些哈希函数来存储。 这个哈希函数本质上是计算对象在表中的位置。 如果我们使用 HashTable 或 HashMap 并且大小不能容纳更多元素,那么这些集合将调整大小以
我不太清楚如何处理编译器发出的不使用 hashValue 而是实现 hash(into:) 的弃用警告。 'Hashable.hashValue' is deprecated as a protoco
我无法理解这一点。 首先我想指出,当我从输入命令行声明这一点时: #define Please 100 #define routine 120 因为“Please”和“routine”具有相同的哈希值
我的 iOS 项目一直面临以下问题(这只是一个警告)。 'Hashable.hashValue' is deprecated as a protocol requirement; conform ty
我最近发现下面的代码以散列冲突告终。 仅供引用,我使用的是 XCode 9.4.1 (9F2000),它使用 Swift 4.1.2 import Foundation let lhs = "あいうえ
在某些情况下和一些代码中,我看到 hashValue 用于将 Bool 转换为 Int。 但是,代码 let someValue = true let someOtherValue = false p
在使用Swift内置的字符串hashValue生成哈希值时,我们发现了两个不同的字符串生成相同哈希值的情况……但是碰撞的原因非常扑朔迷离…… 取这两个字符串: var str1 = """ 00000
我尝试按照本教程进行操作: http://javakart.blogspot.in/2012/12/spring-data-redis-hello-world-example.html 我的问题与此有
我的 iOS 项目一直面临以下问题(这只是一个警告)。 'Hashable.hashValue' is deprecated as a protocol requirement; conform ty
在Swift中为NSObject子类化时,您应该重写哈希还是实现Hashable?此外,你应该重载isEqual:或实现==? 最佳答案 NSObject已经符合所述Hashable协议: exten
如果我有一个字典,例如 { key1 : value1, key2 : value2,..., key17:value17 },并且 2 个键给出相同的散列,比如 key13 和 key5散列时给出
Swift 中的Hashable 协议(protocol)要求您实现一个名为hashValue 的属性: protocol Hashable : Equatable { /// Returns
我有一个“通过 String.hashValue 获取字符串的散列”代码,我在下面添加了它。此代码在 Xcode 9.4.1 中运行良好。 工作良好意味着每当我关闭应用程序并重新打开它时,hashVa
仍在学习 Swift...在为图边设计类时,我想将其抽象并使其成为协议(protocol): protocol GraphEdge: Hashable { typealias Vertex
在 Swift 中子类化 NSObject 时,应该重写 hash 还是实现 Hashable? 此外,您应该重写 isEqual: 还是实现 == 运算符? 最佳答案 NSObject 已经符合 H
Objective-C 中的 .hash 等同于 Swift 中的 .hashValue 吗? 如果不是,Objective-C 中的 .hashValue 是什么? 这与我在这里遇到的一个问题有关(
我是一名优秀的程序员,十分优秀!