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方法的更多相关文章

  1. Java中hashCode()方法以及HashMap()中hash()方法

    Java的Object类中有一个hashCode()方法: public final native Class<?> getClass(); public native int hashC ...

  2. HASH方法课下补分博客

    课堂要求:利用除留余数法为下列关键字集合的存储设计hash函数,并画出分别用开放寻址法和拉链法解决冲突得到的空间存储状态(散列因子取0.75)关键字集合:85,75,57,60,65,(你的8位学号相 ...

  3. 海量数据挖掘MMDS week2: 频繁项集挖掘 Apriori算法的改进:非hash方法

    http://blog.csdn.net/pipisorry/article/details/48914067 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...

  4. __del__,item系列 ,hash方法,__eq__,

    # 构造方法 申请一个空间# 析构方法 释放一个空间 # 某个对象借用了操作系统的资源,还要通过析构方法归还回去:文件资源 网络资源 # 垃圾回收机制 class A: def __del__(sel ...

  5. 同样的输入,为什么Objects.hash()方法返回的hash值每次不一样?

    背景 开发过程中发现一个问题,项目中用Set保存AopMethod对象用于去重,但是发现即使往set中添加相同内容的对象,每次也能够添加成功. AopMethod类的部分代码如下: public cl ...

  6. 利用NSString的Hash方法比较字符串

    实际编程总会涉及到比较两个字符串的内容,一般会用 [string1 isEqualsToString:string2] 来比较两个字符串是否一致.对于字符串的isEqualsToString方法,需要 ...

  7. 数组去重--hash方法

    hash方法我以前百度找到的,经常用性能好速度快,本文章主要是一步步解释hash方法的过程(其实没多少步) 在这里就能看出每个自定义下标都是独一无二的,其实就相当于数组arr已经去重了 剩下我们就需要 ...

  8. js数组去重的hash方法

    对于 JavaScript 数组去除重复项,现在有多种方法,其中一种是hash,如下: if (!Array.prototype.unique) { Array.prototype.unique = ...

  9. NSObject协议中方法:description 和 debugDescription

    description基本概念 1.NSLog(@"%@", objectA);这会自动调用objectA的description方法来输出ObjectA的描述信息. 2.desc ...

随机推荐

  1. SqlServer查看表、存储过程、耗时查询、当前进程、开销较大的语句

    --查看数据库中表的语句 SELECT s2.dbid , DB_NAME(s2.dbid) AS [数据库名] , --s1.sql_handle , ( SELECT TOP 1 SUBSTRIN ...

  2. Simple Web API Server in Golang (1)

    To be an better Gopher, get your hands dirty. Topcoder offered a serials of challenges for learning ...

  3. Codeforces Round #254 (Div. 1) D - DZY Loves Strings

    D - DZY Loves Strings 思路:感觉这种把询问按大小分成两类解决的问题都很不好想.. https://codeforces.com/blog/entry/12959 题解说得很清楚啦 ...

  4. python 实现远端ftp文件上传下载

    python 实现ftp上传下载 * 脚本需要传入两个参数,参数1为需要从远端ftp站点下载文件名称,参数2为已知需要下载的文件md5值,文件下载完成后会自动进行md5值校验 * 运行示例 [root ...

  5. P1244 青蛙过河

    P1244 青蛙过河NOI2000主要思想:数学归纳法 递推 压位高精度 化归 理解能力和找规律的能力题意再述:1.青蛙从上到下必须连续递增或者下面是石墩 而不能是1 12 33 4而且每时每刻都要满 ...

  6. CLR寄宿和AppDomain

    一.CLR寄宿 .net framework在windows平台的顶部允许.者意味着.net framework必须用windows能理解的技术来构建.所有托管模块和程序集文件必须使用windows ...

  7. 【转载】CMenu自绘---钩子---去除边框

    使用默认的CMenu菜单类或者继承CMenu实现的菜单扩展类,在显示的时候最外层都会有边框出现,或者说是具有3D外观(菜单阴影不算),当改变菜单背景色或者需要加个边框线时就会看上去很不美观.看过很多菜 ...

  8. 高性能server分析 - Hadoop的RpcServer

    一.Listener Listener线程,当Server处于运行状态时,其负责监听来自客户端的连接,并使用Select模式处理Accept事件. 同时,它开启了一个空闲连接(Idle Connect ...

  9. FireDAC 下的 Sqlite [9] - 关于排序

    SQLite 内部是按二进制排序, 可以支持 ANSI; FrieDAC 通过 TFDSQLiteCollation 支持了 Unicode 排序, 并可通过其 OnCompare 事件自定义排序. ...

  10. C#基于SMTP协议和SOCKET通信,实现邮件内容和附件的发送,并可隐藏收件人

    经过几天的努力,从完全不懂SMTP到折腾出个可以发送邮件内容和附件的DEMO.话少说,直接上代码. using System; using System.Collections.Generic; us ...