Compare objects
You can isEqual:
compare two objects using a method. The object that receives the message is compared to the incoming object, and if the same, the method returns YES
. For example:
BOOL objectsareequal = [Obj1 isequal:obj2]; |
if (objectsareequal) { |
Do something ... |
} |
Note that object equality is different from the same object. For the latter, use the same operator to ==
test whether two variables point to the same instance.
When you compare two objects of the same class, what exactly are you comparing? This depends on the class. The root class uses NSObject
the pointer equality as the basis for comparison. Subclasses of any level can override the implementation of their superclass to make comparisons based on class-specific criteria, such as Object state. For example, if a hypothetical person object matches the first name, last name, and birth date property of another person object, the two objects may be equal.
The value of the Foundation framework and the set class, declaring the comparison method to the isEqualTo
type :
format, where type is the class type minus the NS prefix, such as isEqualToString:
and isEqualToDictionary:
. This comparison assumes that the incoming object belongs to the given type, otherwise an exception is thrown.
Using sequel to compare objects