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.