I have a class called AddressCard from an example in "Programming in Objective C", and I'm implementing a isEqual: method.
The signature of this method in NSObject uses loose typing for the parameter:
- (BOOL)isEqual:(id)anObject
OTOH, the sample code in the book uses strict typing:
- (BOOL) isEqual:(AddressCard *) aCard
I'm not sure I fully understand what the compiler does in this case. I tried comparing an AddressCard to a NSString ([aCard isEqual: @"Foo"]
) expecting either a runtime error (if the system uses my method) or that the system would call NSObject's version of IsEqual.
Instead, my method was called (even though the parameter was a NSString and not an AddressCard) and raised an exception when my IsEqual: tried to call a a method specific to AddressCard:
- (BOOL) isEqual:(AddressCard *) aCard {
if ([name isEqualToString: [aCard name]] && /*here I get the error*/
[email isEqualToString:[aCard email]]) {
return YES;
}else {
return NO;
}
}
What's going on? How on Earth is an NSString being passed to a method that expects something else? Is changing the signature of a method OK when overriding it?