I'm having some difficulties adding custom equality checks to one of my own objects.
I have a custom Message object which has a unique messageID property which is an NSString, as well as other information about the message.
The objects are being stored in an NSMutableSet, so uniqueness is required in the collection, but I need them to be unique based on the messageID, not the default.
I have added the following code in order to do so.
- (BOOL)isEqual:(id)object
{
if (![object isKindOfClass:Message.self]) {
return false;
}
if (object == self) {
return true;
}
Message *otherMessage = (Message *)object;
if ([otherMessage.messageID isEqualToString:self.messageID]) {
return true;
}
return false;
}
- (NSUInteger)hash
{
return [self.messageID hash];
}
However when I start adding instances of Message objects to a set, the app (sometimes, not always) crashes on one of the following lines:
if ([otherMessage.messageID isEqualToString:self.messageID]) {
OR
return [self.messageID hash];
Basically whenever it tries to access the messageID property.
And the error I get is EXC_BAD_ACCESS.
Any pointers in the right direction would be greatly appreciated!