Iglistkit: Question about an assert in IGListAdapter.m

Created on 6 Jan 2017  路  4Comments  路  Source: Instagram/IGListKit

This is a little strange.

#if DEBUG
    for (id object in objects) {
        IGAssert([object isEqual:object], @"Object instance %@ not equal to itself. This will break infra map tables.", object);
    }
#endif

When will [object isEqual:object] return NO? object won't be nil in the for-loop.
Could you please attach an example for this?

question

All 4 comments

@PhilCai1993 before ab890fc6070f170a2db5a383a6296e62dcf75678 when we used -isEqual: to test diffable objects, we had a user bug where someone's equality implementation accidentally returned NO even when the object was the same pointer. The error was really difficult to track down, so we added this assert so it wont even happen again.

However, since we changed the equality method to -isEqualToDiffableObject:, this assert should be updated.

But I'm still confused...
How could [obj isEqualToDiffableObject:obj ] return NO?

-(BOOL)isEqualToDiffableObject:(id)object {
//   it returns NO when object === self, how could it happen?
}

Is there an issue which is related to the bug you metioned?

@PhilCai1993 Purely by developer error. Example:

#import <Foundation/Foundation.h>

@interface MyClass: NSObject
@property NSString *text;
@end

@implementation MyClass
- (BOOL)isEqualToDiffableObject:(id)object {
  if (![object isKindOfClass:[MyClass class]]) { return NO; }
  return [self.text isEqualToString:[object text]];
}
@end

int main(int argc, char *argv[]) {
  @autoreleasepool {
    MyClass *left = [MyClass new];
    NSLog(@"%zi", [left isEqualToDiffableObject:left]);
    // prints "0" (aka NO)
  }
}

That's b/c passing a message to nil returns a 0 value (NO in this case). Obviously this is fixed with a self == object check, but people make mistakes. The assert just makes a tricky-to-catch mistake impossible while debugging.

Thanks a lot!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iwasrobbed picture iwasrobbed  路  3Comments

jessesquires picture jessesquires  路  3Comments

lucabartoletti picture lucabartoletti  路  3Comments

joseph-francis picture joseph-francis  路  3Comments

racer1988 picture racer1988  路  3Comments