我有一个覆盖 isEqual 方法的人员类:
#import "People.h"
@implementation People
@synthesize name = _name;
-(BOOL)isEqual:(id)object {
NSLog(@"A.isEqual called");
if ([object isKindOfClass:[NSString class]]) {
NSString *name = object;
return [self.name isEqualToString:name];
}
return [super isEqual:object];
}
@end
当我在数组中找到人时,它不会调用 isEqual 方法:
People* p1 = [People new];
p1.name = @"A";
People* p2 = [People new];
p2.name = @"B";
NSArray* peopleArray = [NSArray arrayWithObjects:p1, p2, nil];
NSLog(@"find A %ld", [peopleArray indexOfObject:@"A"]);
NSLog(@"isEqual A -> %@", [p1 isEqual:@"A"] ? @"Yes" : @"No");
这是输出:
2013-04-10 18:15:47.988 runone[6273:303] find A 9223372036854775807
2013-04-10 18:15:47.988 runone[6273:303] A.isEqual called
2013-04-10 18:15:47.989 runone[6273:303] isEqual A -> Yes
你知道这是为什么吗?