星空网 > 软件开发 > 操作系统

NSObject的hash方法

NSObject的hash方法

NSObject的hash方法images/loading.gif' data-original="http://images2015.cnblogs.com/blog/607542/201604/607542-20160415220445441-1616011676.gif" />

 

说明

本示例仅仅演示一个对象什么时候执行hash方法。

 

细节

1. 必要的Model类,重载了hash方法用以反映Hash方法是否被调用了

NSObject的hash方法

NSObject的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

NSObject的hash方法

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

NSObject的hash方法

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

NSObject的hash方法

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

NSObject的hash方法

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

NSObject的hash方法

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

NSObject的hash方法

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

NSObject的hash方法

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

NSObject的hash方法

 

总结

一个对象在用作key值时,其 hash 方**被调用,用以生成一个唯一标识符,NSDictionary 需要根据唯一 key 值(根据 hash 算法生成的值)查找对象, NSSet 需要根据 hash 值来确保过滤掉重复的对象。




原标题:NSObject的hash方法

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

专线墨西哥:https://www.goluckyvip.com/tag/49604.html
专线欧洲:https://www.goluckyvip.com/tag/49605.html
专线沙特:https://www.goluckyvip.com/tag/49606.html
专线双清:https://www.goluckyvip.com/tag/49607.html
专线泰国:https://www.goluckyvip.com/tag/49608.html
专线物流查询:https://www.goluckyvip.com/tag/49609.html
夹江千佛岩景区门票(夹江千佛岩景区门票价格):https://www.vstour.cn/a/411232.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流