NSObject的hash方法
NSObject的hash方法

说明
本示例仅仅演示一个对象什么时候执行hash方法。
细节
1. 必要的Model类,重载了hash方法用以反映Hash方法是否被调用了


2. 测试
//
// ViewController.m
// Hash
//
// Created by YouXianMing on 16/4/15.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "Model.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Model *model = [Model new]; [model hash];
model = nil;
} @end

3. 测试 isEqual: 方法执行的时候是否会执行 hash 方法,打印情况里面是没有的
//
// ViewController.m
// Hash
//
// Created by YouXianMing on 16/4/15.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "Model.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Model *modelA = [Model new];
Model *modelB = [Model new]; if ([modelA isEqual:modelB]) { NSLog(@"YES"); } else { NSLog(@"NO");
}
} @end

4. 用 NSMutableSet 添加对象,这时候会执行hash方法,至于为何会执行2回 _(:з」∠)_ ?
//
// ViewController.m
// Hash
//
// Created by YouXianMing on 16/4/15.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "Model.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Model *model = [Model new];
NSMutableSet *set = [NSMutableSet set]; [set addObject:model];
} @end

5. 用 NSMutableArray 添加对象测试一下,发现不会执行 hash 方法
//
// ViewController.m
// Hash
//
// Created by YouXianMing on 16/4/15.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "Model.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Model *model = [Model new];
NSMutableArray *array = [NSMutableArray array]; [array addObject:model];
} @end

6. 用作 NSMutableDictionary 中的 object 时,hash 方法不会执行
//
// ViewController.m
// Hash
//
// Created by YouXianMing on 16/4/15.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "Model.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Model *model = [Model new];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:model forKey:@"A"];
[dictionary objectForKey:@"A"];
} @end

7. 用作 NSMutableDictionary 中的 key 时,hash 方法执行了,不过崩溃了,因为 Model 类没有实现 NSCopying 协议
//
// ViewController.m
// Hash
//
// Created by YouXianMing on 16/4/15.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "Model.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Model *model = [Model new];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:@"A" forKey:model];
} @end

8. NSSet 在初始化的时候添加了 model 并不会让 model 执行 hash 方法
//
// ViewController.m
// Hash
//
// Created by YouXianMing on 16/4/15.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "Model.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Model *model = [Model new]; NSSet *set = [NSSet setWithObjects:model, nil]; if ([[set anyObject] isEqual:model]) { NSLog(@"A");
} set = nil;
} @end

9. 在创建不可变数组时,model 作为 key 会执行 hash 方法,但同样会崩溃,因为 Model 类没有实现 NSCopying 协议
//
// ViewController.m
// Hash
//
// Created by YouXianMing on 16/4/15.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "Model.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Model *model = [Model new];
NSDictionary *dictionary = @{model : @"A"};
dictionary = nil;
} @end

总结
一个对象在用作key值时,其 hash 方法会被调用,用以生成一个唯一标识符,NSDictionary 需要根据唯一 key 值(根据 hash 算法生成的值)查找对象, NSSet 需要根据 hash 值来确保过滤掉重复的对象。
NSObject的hash方法的更多相关文章
- Java中hashCode()方法以及HashMap()中hash()方法
Java的Object类中有一个hashCode()方法: public final native Class<?> getClass(); public native int hashC ...
- HASH方法课下补分博客
课堂要求:利用除留余数法为下列关键字集合的存储设计hash函数,并画出分别用开放寻址法和拉链法解决冲突得到的空间存储状态(散列因子取0.75)关键字集合:85,75,57,60,65,(你的8位学号相 ...
- 海量数据挖掘MMDS week2: 频繁项集挖掘 Apriori算法的改进:非hash方法
http://blog.csdn.net/pipisorry/article/details/48914067 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...
- __del__,item系列 ,hash方法,__eq__,
# 构造方法 申请一个空间# 析构方法 释放一个空间 # 某个对象借用了操作系统的资源,还要通过析构方法归还回去:文件资源 网络资源 # 垃圾回收机制 class A: def __del__(sel ...
- 同样的输入,为什么Objects.hash()方法返回的hash值每次不一样?
背景 开发过程中发现一个问题,项目中用Set保存AopMethod对象用于去重,但是发现即使往set中添加相同内容的对象,每次也能够添加成功. AopMethod类的部分代码如下: public cl ...
- 利用NSString的Hash方法比较字符串
实际编程总会涉及到比较两个字符串的内容,一般会用 [string1 isEqualsToString:string2] 来比较两个字符串是否一致.对于字符串的isEqualsToString方法,需要 ...
- 数组去重--hash方法
hash方法我以前百度找到的,经常用性能好速度快,本文章主要是一步步解释hash方法的过程(其实没多少步) 在这里就能看出每个自定义下标都是独一无二的,其实就相当于数组arr已经去重了 剩下我们就需要 ...
- js数组去重的hash方法
对于 JavaScript 数组去除重复项,现在有多种方法,其中一种是hash,如下: if (!Array.prototype.unique) { Array.prototype.unique = ...
- NSObject协议中方法:description 和 debugDescription
description基本概念 1.NSLog(@"%@", objectA);这会自动调用objectA的description方法来输出ObjectA的描述信息. 2.desc ...
随机推荐
- Linux 基础——权限管理命令chmod
一.Linux中的文件权限与目录权限 Linux中定义了3种访问权限,分别是r.w.x.其中r表示对象是可读的,w表示对象是可写的,x表示对象是可执行的,这3种权限组成一组rwx分别对应对象的3个安全 ...
- SqlServer查看表、存储过程、耗时查询、当前进程、开销较大的语句
--查看数据库中表的语句 SELECT s2.dbid , DB_NAME(s2.dbid) AS [数据库名] , --s1.sql_handle , ( SELECT TOP 1 SUBSTRIN ...
- node 使用笔记
1 安装 buffertools 因为使用mjpeg-proxy的关系,需要编译buffertools中的C++代码文件,怎奈何一直出错. make: Entering directory `/srv ...
- PyTorch-Kaldi 语音识别工具包
翻译: https://arxiv.org/pdf/1811.07453.pdf ABSTRACT 开源软件的可用性在语音识别和深度学习的普及中发挥了重要作用.例如,Kaldi 现在是用于开发最先进 ...
- 使用VS2013、TFS2013和Git进行分布式团队协作
题记:呵呵,首先声明,题目起的有点大,其实我只想介绍下VS2013和TFS2013新加入的Git功能,也不是在VS中使用Git的详细向导(以后有空再详细分享给大家).这篇文章虽然在写这篇文章<V ...
- 【LOJ】#6437. 「PKUSC2018」PKUSC
题解 我们把这个多边形三角形剖分了,和统计多边形面积一样 每个三角形有个点是原点,把原点所对应的角度算出来,记为theta 对于一个点,相当于半径为这个点到原点的一个圆,圆弧上的弧度为theta的一部 ...
- OLAP和OLTP基础知识
数据处理大致可以分成两大类:联机事务处理OLTP(on-line transaction processing).联机分析处理OLAP(On-Line Analytical Processing).O ...
- linux学习笔记-5.用户和组
1.添加一个tom用户,设置它属于users组,并添加注释信息 分步完成: useradd tom usermod -g users tom usermod -c "hr tom" ...
- makefile 必知必会以及Makefile是怎样炼成的
Make必知必会原文链接 Makefile 必知必会 Makefile的根本任务是根据规则生成目标文件. 规则 一条规则包含三个:目标文件,目标文件依赖的文件,更新(或生成)目标文件的命令. 规则: ...
- 实现常用的配置文件/初始化文件读取的一个C程序
在编程中,我们经常会遇到一些配置文件或初始化文件.这些文件通常后缀名为.ini或者.conf,可以直接用记事本打开.里面会存储一些程序参数,在程序中直接读取使用.例如,计算机与服务器通信,服务器的ip ...