I. The role of IsEqual
The role of isequal: To determine whether two objects are equal
For the base type, the = = operator compares the value;
For object types, the = = operator compares the address of an object (that is, whether it is the same object)
Objective-c and Java do not support operator overloading, and C + + supports operator overloading, so that you can make a judgment on operator overloading Two, rewrite the IsEqual method
The type defined in the COCOA framework,
NSString isequaltostring
NSDate isequaltodate
Nsarray Isequaltoarray
Nsdictionary isequaltodictionary
Nsset Isequaltoset
The IsEqual method has been implemented.
For custom types, it is often necessary to override the IsEqual method
First define the Testmodel class as follows
@interface testmodel:nsobject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *color;
@end
-(BOOL) IsEqual: (ID) object {
if (self = = object) {return
YES;
}
if (![ Object Iskindofclass:[testmodel class]] {return
NO;
}
return [self Isequaltotestmodel: (Testmodel *) object];
-(BOOL) Isequaltotestmodel: (Testmodel *) TModel {
if (!tmodel) {return
NO;
}
BOOL equalname = (!self.name &&!tmodel.name) | | [Self.name IsEqualToString:tModel.name];
BOOL Equalcolor = (!self.color &&!tmodel.color) | | [Self.birthday IsEqualToString:tModel.color];
return equalname && equalcolor;
}
Main steps:
1: = = operator to determine whether the same object, the same object must be exactly the same
2: Determine whether the same type, you can improve the efficiency of the sentence, but also to avoid implicit type conversion potential risks
3: Improve code reusability by encapsulating the Isequaltotestmodel method
4: Judge whether Testmodel is nil, do parameter validity check
5: Each attribute uses the default method and so on to judge separately
6: Return all attributes of the sentence and the result of three, rewrite the hash method
The hash method is invoked only when the object is added to the Nsset and the key that is set to Nsdictionary.
Nsset add a new member, you need to quickly find a member based on the hash value to ensure that the member is already present in the collection
Nsdictionary to find key, also use the hash value of the key to improve the efficiency of the search
The hash method is used primarily for querying members in hash table.
In order to optimize the efficiency of the sentencing, Nsset and nsdictionary based on hash will do so when judging whether members are equal
The first step: whether the hash value of the set member is equal to the target hash value, if the same goes into the second step to judge, if unequal, the direct judgment is not equal
The second step: the same hash value (the first step) in the case, and then the object of the sentence, as a result of the sentence
Hash value is a necessary and not sufficient condition for object judgment
-(Nsuinteger) hash {return
[super hash];
}
This rewrite the hash method, there is a hidden danger.
//[super Hash] Returns the memory address of the object
//If two object addresses are different, but the content is exactly the same, you can join Nsset at this point.
Mattt Thompson's conclusion in equality is that
In reality, a simple XOR over the hash values of critical properties are sufficient 99% of the time (a bit or an operation on the hash value of a key attribute as a hash Value
For Testmodel
-(Nsuinteger) hash {return
[self.name hash] ^ [self.color hash];
}
Reference article:
mattt Thompson
Equality
NSObject Hash method
Shallow copy deep copy and hash/equal