Today I wanted to override the <code>- (void) isEqual:(id)</code> method of a 
custom, but very simple subclass of NSObject:

<code>
@interface Resolution : NSObject {
NSUInteger mWidth;
NSUInteger mHeight;
}
-(id) initWithWidth:(NSUInteger) width andHeight:(NSUInteger) height;
-(BOOL) isValidResolution;

@property NSUInteger width;
@property NSUInteger height;
@end
</code>

All the custom implementation of <code>- (void) isEqual:(id)</code> performs, 
is to compare width and height.

This simple comparison failed. To find out, where I expanded it to 

<code> 
- (BOOL)isEqual:(id)object {
        if ([object isKindOfClass:[Resolution class]]){
                NSUInteger oWidth = [object width];
                NSUInteger oHeight = [object height];
                NSUInteger sWidth = [self width];
                NSUInteger sHeight = [self height];
                BOOL isSameWidth = oWidth == sWidth;
                BOOL isSameHeight = oHeight == sHeight;
                return isSameWidth && isSameHeight;
        }
        return NO;
}
</code>

Surprisingly, oWidth was 0 instead of the expected value of 300. BUT, according 
to gdb, object.mWidth was 300.

See a screenshot in this blog post: http://freies-blasen.de/?p=39 

Finally, the method succeeded, when it was changed to 

<code>
- (BOOL)isEqual:(id)object {
        if ([object isKindOfClass:[Resolution class]]){
                Resolution *cmpRes = (Resolution*) object;
                NSUInteger oWidth = cmpRes.width;
                NSUInteger oHeight = cmpRes.height;
                NSUInteger sWidth = self.width;
                NSUInteger sHeight = self.height;
                BOOL isSameWidth = oWidth == sWidth;
                BOOL isSameHeight = oHeight == sHeight;
                return isSameWidth && isSameHeight;
        }
        return NO;
}
</code>

Who can explain this behavior to me? Why is oWidth != object.mWidth ? How can 
that happen?

Thanks,
 Philipp
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to