设为首页 加入收藏

TOP

NSObject的hash方法(一)
2017-10-13 10:17:21 】 浏览:8495
Tags: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 = [NSMutableDictiona
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇我对XCode Objective-c Cocoa的简.. 下一篇iOS的URL处理

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目