Re: Property synthesis trouble - 32 vs 64 bit builds
Re: Property synthesis trouble - 32 vs 64 bit builds
- Subject: Re: Property synthesis trouble - 32 vs 64 bit builds
- From: Keary Suska <email@hidden>
- Date: Tue, 05 Jul 2016 09:12:30 -0600
> On Jul 5, 2016, at 5:36 AM, Jonathan Taylor <email@hidden> wrote:
>
> I have a problem with property synthesis in my code, which I hope somebody can advise on. I find I have to write different property synthesis code for 32- or 64-bit builds in order to avoid compiler errors.
>
> A minimal demonstration of the problem can be found below - build as part of a fresh project on Xcode 5 or 6, with ARC disabled. It surprises me that I have to write different synthesis code for 32- or 64-bit builds - and this then makes me worry that I am doing something more fundamentally wrong. Can anyone comment (or explain)?
>
> [I appreciate that this code may be old-fashioned in style - I am not even sure what is or is not the recommended style these days. It seems to work though - and, importantly, as far as I can tell the synthesized property myFlag on MutableSettings *does* map to the same backing variable, even on a 64-bit build where I am not able to specify the backing variable in the synthesis statement]
32 vs 64 is likely coincidental, and probably has more to do with compiler differences. I.e., at that time, there were additional compiler “features” for 64 bit vs 32 bit. So my thought is that what you are doing is wrong in both cases, but you are simply getting different compiler errors.
The essential issue I believe is that you cannot re-synthesize in a subclass. You will have to specify the setter explicitly. The issue in the second (64-bit) case is that when you specify @synthesize with an instance variable you are specifying a *new* instance variable on that class, but the compiler correctly warns you that that instance variable has already been defined in the superclass and therefore cannot also be defined in the subclass. The 32 bit compiler seems to be letting you get away with this error, but it could have horrible unintended consequences.
> @interface ImmutableSettings : NSObject
> {
> BOOL _myFlag;
> }
> @property (readonly) BOOL myFlag;
> @end
>
> @interface MutableSettings : ImmutableSettings
> @property (readwrite) BOOL myFlag;
> @end
>
> @implementation ImmutableSettings
>
> -(id)init
> {
> if (!(self = [super init]))
> return nil;
> _myFlag = true;
> return self;
> }
> @synthesize myFlag = _myFlag;
>
> @end
>
> @implementation MutableSettings
> // This is very odd - I get errors on 64-bit builds if I specify a backing variable for synthesis,
> // but I get errors on 32-bit builds if I *don't* specify a backing variable!?
> #if defined(__x86_64__)
> // On 32-bit builds I get an error here:
> // "Synthesized property 'myFlag' must either be named the same as a compatible instance variable or must explicitly name an instance variable"
> @synthesize myFlag;
> #else
> // On 64-bit builds I get an error here:
> // "Property 'myFlag' attempting to use instance variable '_myFlag' declared in super class 'ImmutableSettings'"
> @synthesize myFlag = _myFlag;
> #endif
HTH,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
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