NSObject hash method, NSObjecthash Method

Source: Internet
Author: User

NSObject hash method, NSObjecthash Method

NSObject hash Method

 

Description

This example only shows when an object executes the hash method.

 

Details

1. The necessary Model class is overloaded with the hash method to reflect whether the Hash method has been called.

2. Test

/// 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. Test isEqual: whether the hash method is executed when the method is executed. It is not displayed in the print.

/// 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. When NSMutableSet is used to add an object, the hash method is executed. Why does the system execute 2 back_(: required "rows )_?

/// 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. Use NSMutableArray to add an object for testing. The hash method is not executed.

/// 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. When used as an object in NSMutableDictionary, the hash method will not be executed

/// 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. When used as the key in NSMutableDictionary, the hash method is executed, but it crashes because the Model class does not implement the NSCopying protocol.

/// 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. Adding a model to the NSSet during initialization does not allow the model to execute the hash method.

/// 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. When creating an immutable array, the model runs the hash method as the key, but it also crashes because the Model class does not implement the NSCopying protocol.

/// 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

 

Summary

When an object is used as a key value, its hash method is called to generate a unique identifier. NSDictionary needs to find the object based on the unique key value (value generated based on the hash algorithm, NSSet needs to filter out repeated Objects Based on the hash value.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.