• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Strange property/synthesized accessor behaviour
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Strange property/synthesized accessor behaviour


  • Subject: Strange property/synthesized accessor behaviour
  • From: Philipp Leusmann <email@hidden>
  • Date: Sat, 09 Apr 2011 16:52:36 +0200

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

  • Follow-Ups:
    • Re: Strange property/synthesized accessor behaviour
      • From: "Stephen J. Butler" <email@hidden>
    • RE: Strange property/synthesized accessor behaviour
      • From: Lee Ann Rucker <email@hidden>
  • Prev by Date: Re: How To Increment CALayer Retain Count?
  • Next by Date: RE: How To Increment CALayer Retain Count?
  • Previous by thread: Re: Using selectedMenuItemColor
  • Next by thread: RE: Strange property/synthesized accessor behaviour
  • Index(es):
    • Date
    • Thread