RE: Strange property/synthesized accessor behaviour
RE: Strange property/synthesized accessor behaviour
- Subject: RE: Strange property/synthesized accessor behaviour
- From: Lee Ann Rucker <email@hidden>
- Date: Sun, 10 Apr 2011 00:55:52 -0700
- Acceptlanguage: en-US
- Thread-topic: Strange property/synthesized accessor behaviour
Unless you've got
@synthesize width = mWidth;
ObjC has created a second set of variables for you named width and height with no connection to mWidth and mHeight.
________________________________________
From: cocoa-dev-bounces+lrucker=email@hidden [cocoa-dev-bounces+lrucker=email@hidden] On Behalf Of Philipp Leusmann [email@hidden]
Sent: Saturday, April 09, 2011 7:52 AM
To: email@hidden
Subject: Strange property/synthesized accessor behaviour
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 (email@hidden)
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:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden