The definition of == is correct, it checks to see that they're pointing to the actual same pointer/memory address (ie. 0xffffff
)
The key to understanding what you're asking is to think about what you mean by the word "equal". "equal" typically means, from the developer's point of view, that "these two objects contain the same data in the fields that I require for all practical purposes". You can have two user
objects each with the same userID
property but different times in lastUpdated
- would you consider them equal? Depends on your use case. Most likely you would say yes because they're the same user. They were updated from the server at different times, so some fields differ, but for your implementation, they're equal.
In the case above, are they the same object? Definitely not. They point to different memory addresses. So ==
would be NO
, whereas if you wrote your isEqual:
method to check just the userID
property, it would return YES
The definition of isEqual:
is entirely up to the author of the class. isEqual:
can be written to use ==
if you wanted. All you have to do, in your class, is to override the isEqual:
method which is defined by the NSObject
protocol.
If you have a custom object, use isEqual:
to define what your definition of equal is. In the example of a user object, you might define:
- (BOOL)isEqual:(id)otherObject {
if ([otherObject isKindOfClass:[self class]]) {
MyClass *otherObjectAfterCast = (MyClass*)otherObject;
if ([otherObjectAfterCast.userID isEqualToString:self.userID])
return YES;
}
return NO;
}
Technically you'd probably want to use caseInsensitiveCompare:
or something like that but you get the drift...
isEqual:
can also be used to trigger other methods - in the case of NSString
- calling isEqual:
when both operands are strings results in a call to isEqualToString:
- which is why the documentation recommends calling isEqualToString:
if you know they're both strings, since it's a bit faster.
So, isEqual:
is whatever you make of it, or whatever the class author has defined it to be.
This is also a pretty clear definition in the docs (for once lol): NSObject Protocol Reference
Hope this helps! Let me know if you need any further clarification.