Tell me more ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Have just learned that in certain instances, properties (which are auto-generated setters and getters for ivars) don't need declared ivars.

My understanding is that because of encapsulation, only properties are allowed to directly interact with ivars.

So how can you have properties if there aren't declared ivars? How would I know when to use a property without ivars? Are there any guidelines you use?

Thank you very much.

share|improve this question
Sorry, questions about development and programming are off-topic here. See our FAQ for more information. You can ask these questions on StackOverflow – daviesgeek Jul 23 '12 at 3:45

closed as off topic by Nathan Greenstein Jul 23 '12 at 3:38

Questions on Ask Different are expected to relate to Apple hardware or software within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

This probably belongs on Stack Overflow, but I believe the short version is this:

  • In addition to creating the setter and getter methods, @property tells the compiler to create an instance variable behind the scenes, as if you did so manually. In fact you can even give it your own name the @synthesize statement.

  • Any method in an object can access that object's instance variable, including the ones generated by @property. However it's best practice to use the @property, and only directly access the backing instance variables when absolutely necessary, such as within a custom setter or getter method.

For more information, I suggest you check out the Declared Properties page on Apple's Developer site.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.