• 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
RE: Strange property/synthesized accessor behaviour
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >Strange property/synthesized accessor behaviour (From: Philipp Leusmann <email@hidden>)

  • Prev by Date: RE: How To Increment CALayer Retain Count?
  • Next by Date: Re: Strange property/synthesized accessor behaviour
  • Previous by thread: Strange property/synthesized accessor behaviour
  • Next by thread: Re: Strange property/synthesized accessor behaviour
  • Index(es):
    • Date
    • Thread