Benjohn
Benjohn

Reputation: 13887

Preventing Automatic ivar Synthesis of @property

I have readonly properties that I would like to declare similarly to other properties:

@property (nonatomic, readonly) MyThing *thing;

Frequently, I definitely do not want an instance variable and getter method to be automatically synthesised for these properties. In such cases I have made an error if I forget to write an explicit getter for them, and I want the compiler to tell me and refuse to compile (or at least, issue a warning).

In such cases, is it bad practice to use a @property declaration? Should I instead use:

-(MyThing*) myThing;

Or, alternatively, is there some way that I can tell @property in the attributes list (nonatomic, readonly, dont_synthesize_this_one_please) that I definitely don't want an instance variable to be sythesized and it's an error if I've missed out the getter?

Upvotes: 2

Views: 329

Answers (1)

Sulthan
Sulthan

Reputation: 130092

No, there is no special attribute. If you don't overwrite the getter, the ivar will be synthesized.

Making the property a normal method as you suggest is the best solution. You could also experiment with turning on the following compiler warning:

Implicit Synthesized Properties

but then you will get warnings for ALL the synthesized properties.

You can also turn it on for every file separately -Wobjc-missing-property-synthesis

Upvotes: 1

Related Questions