Search code examples
objective-cnsset

NSet containsObject is giving unpredictable results


In my following code [NSSet containsObject] is giving unpredictable results. I find out that Cell3 object with value row,col (2,2) gets added twice thorough my while loop. However if I set breakpoint inside the method isEqual on the line of if condition then (2,2) does not get added twice.

Is this issue related to code optimizations. When I use to work in C++ long time back, I saw these kinds of issues in debug vs release mode. If you set breakpoints then the issue does not happen but if no breakpoint then the issue happens. I have never seen this kind of issue in XCode. I am using XCode version 9.2(9C40b)

@interface Cell3 : NSObject

@property(assign) NSInteger row;
@property(assign) NSInteger col;

-(instancetype) initWithRow:(NSInteger)row col:(NSInteger)col;
@end


@implementation Cell3

-(instancetype) initWithRow:(NSInteger)row col:(NSInteger)col
{
    self = [super init];
    if(self)
    {
        self.row = row;
        self.col = col;
    }
    return self;
}

- (BOOL)isEqual:(Cell3*)object
{
    if(self.row == object.row && self.col == object.col)
        return YES;
    else
        return NO;
}

@end


int NSSetTest()
{
    NSMutableSet *uniqueCells = [[NSMutableSet alloc]init];

    int a = 0;

    int b[9][2] = { {0,0}, {1,0}, {2,0}, {3,0}, {3,1}, {3,2}, {2,2}, {2,3}, {2,2} };

    while(a < 9)
    {
        Cell3 *cell = [[Cell3 alloc] initWithRow:b[a][0] col:b[a][1]];

        if(![uniqueCells containsObject:cell])
        {
            [uniqueCells addObject:cell];
        }
        a++;
    }

    return 0;
}

Solution

  • The problem is that your Cell3 class lacks an appropriate hash implementation. See https://developer.apple.com/documentation/objectivec/1418956-nsobject/1418859-hash