0

I've encountered some strange behavior from an NSMutableDictionary that I'm using in a program of mine, in which a lookup in the dictionary returns nil despite the object being present and the two keys returning true for an -isEqual: call (which I have overridden for MyClass). What could be going on? Am I missing something about using custom classes in NSDictionaries?

(lldb) p [(MyClass *)self.dict.allKeys[0] isEqual:object]
-> (BOOL) $7 = YES

(lldb) po self.dict[self.dict.allKeys[0]]
-> My Object Description!

(lldb) po self.dict[object]
-> nil

EDIT: Implementation for -copyWithZone: and -isEqual:

//MyClass.m
-(BOOL)isEqual:(id)object {
    if ([object isKindOfClass:[MyClass class]]) {
        if(((MyClass *)object).someProperty == self.someProperty) { //someProperty is an enum type so direct comparison is fine
            return YES;
        }
        return NO;
    }
    return NO;
}

-(id)copyWithZone:(NSZone *)zone {
    MyClass *copy = [[[self class] alloc] init];
    if (copy) {
        copy.someProperty = self.someProperty; //again, someProperty is an enum type, no need to copy recursively
    }
    return copy;
}
Jumhyn
  • 6,687
  • 9
  • 48
  • 76

0 Answers0