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 ...
随机推荐
- Qt编程之悲惨世界
最近需要给人写点基于QtWebkit的代码,算是领教了Qt编程的痛苦之处. QNetworkConfigurationManager::isOnline() 只有在编译平台上能运行,拷贝到其他Wind ...
- IdentityServer4结合AspNetCore.Identity实现登录认证踩坑填坑记录
也可以自定义实现,不使用IdentityServer4.AspNetIdentity这个包,当然还要实现其他接口IResourceOwnerPasswordValidator. IProfileSer ...
- codevs 1795 金字塔 2
codevs 1795 金字塔 2这个题比完全背包多了一个总数的限制,即一定要选(m+n)个,题中说总重量不超过n,所以至少选择m个重量为0的,然后初始化的时候,都填成重量为0的,然后再一个个地把它们 ...
- django 的用户验证及登录状态保持
一.用户验证功能 Django自带用户验证及登录功能,引入模块为: from django.contrib.auth import authenticate 其中方法authenticate()的接收 ...
- 样式加载不出来,浏览器控制台报错:Resource interpreted as Stylesheet but transferred with MIME type text/html
写登录的时候出现的问题,样式时好时坏,浏览器控制台看到的信息是: Uncaught SyntaxError: Unexpected token <Resource interpreted as ...
- PBR Step by Step(四)Lambertian反射模型
光照可分为局部光照和全局光照. 局部光照:直接照射到物体表面的光照 全局光照:物体表面受周围环境影响的光照 左图中点x接收到周围环境的光线照射,来自周围表面的反射光照称为全局光照:右图中点x接收来自太 ...
- WEP保护帧移除工具airdecloak-ng
WEP保护帧移除工具airdecloak-ng 为了防止WEP加密数据被破解,WIPS(无线入侵防御系统)会发送WEP保护帧.攻击者抓取WEP数据包时,也会获取这一类包,导致破解失败.aircra ...
- Windows7双系统的启动顺序怎样修改?
本着工作的原因或个人的原因,不过绝大部分还是因为个人怀旧的因素比较多.大家即使安装了新的Windows 7,可是又不想放弃原来的xp765系统,安装双系统就成为不少人的选择.不过有一个麻烦,那就是系统 ...
- web前端面试经历分享
十天前,我还在纠结这个暑假到底是呆在实验室研究技术好还是找一份实习见识世面好,而现在我已经接到offer准备工作了.这几天真是累得够呛,一方面需要拼命准备期末考试,另一方面,需要往公司里面跑接受面试. ...
- BZOJ.3990.[SDOI2015]排序(DFS)
题目链接 操作序列的顺序显然是无关的,所以只需按特定顺序求出一个长度为\(l\)的操作序列,它对答案的贡献为\(l!\). 我们从小到大枚举所有选择.若当前为第\(i\)个,如果有一段长度为\(2^i ...