[This post is intended only for audiences with Basic knowledge in Objective-C]
With Cocoa / UIKit framework has built-in support for comparing objects like NSDate, NSString, NSData, NSNumber and few others which would help us to identify whether two objects are EQUAL, GREATER THAN or LESSER THAN the other object. This in-turn is used in cases like sorting, searching.
We would like to see how the same can be achieved for the custom classes we write, where we can specify the Equality condition by our own.
By default, NSObject class allows us to determine equality between two objects whose class is derived from NSObject through
where Apple documentation defines it as
“This method defines what it means for instances to be equal. For example, a container object might define two containers as equal if their corresponding objects all respond YES to an isEqual: request. See the NSArray, NSDictionary, NSString and NSData class specifications for examples of the use of this method.
If two objects are equal, they must have the same hash value. This last point is particularly important if you define isEqual: in a subclass and intend to put instances of that subclass into a collection. Make sure you also define hash in your subclass. “
In short, At NSObject (root class) level, two objects are said be equal
- if their hash value are same
- two objects have same address.
Well this may not be the case when we write our custom class, for example, two NSString objects might have different pointer addresses, but have same string value. Hence, one must override hash method and isEqual: method of NSObject. NSString class would have hash and isEqual defined as below
//return the hash value generated from the string.
// return strcmp ( [self’s cString] , [otherObject’s cString] )
Where do I apply this?
Well, if you encounter a situation where you have an array (or in general collection) of Employees for eg, and you would want to remove an employee of particular ID, then you can do it with a single call like this. This would save you from writing an search algorithm.
[myEmployees removeObject: employeeToRemove]; // assuming myEmployees is of type NSMutableArray
As Apple document says, removeObject and its variants (see below list for variants) would pick the object to remove for which isEqual: would return TRUE
- – removeObject:
- – removeObjectIdenticalTo:
- – removeObjectIdenticalTo:inRange:
- – removeObjectsInArray:
- – indexOfObject:
- – indexOfObject:inRange:
- – indexOfObjectIdenticalTo:
- – indexOfObjectIdenticalTo:inRange:
Another method that does fall in this category, is compare: method (actually you can give a custom name to this method ) whose signature is
NSComparisonResult is an enum with values as below
NSOrderedAscending = -1,
With this method implemented in your Custom class (for example, Employee class), you can sort your objects in a collection (NSMutableArray , NSArray) with a single statement
[ myEmployee sortArrayUsingSelector(compare:)];
Other variants where this strategy can be used are
Hope this tutorial find useful for you.
Happy Coding 🙂