views:

258

answers:

3

From what I've read, @properties and @synthesize are used to tell the compiler to to generate getters and setters for various properties of our class.

So I am confused when I see the source code of an Apple example, the GKTank, that uses only one class for the whole game (apart from the app delegate) and still most of the attributes of that class are defined in @property() and are synthesized, even if no other class will try to get/set them. What am I misunderstanding here?

Thanks

+5  A: 

Using properties is generally good practice as the synthesized setters will do the right thing when it comes to memory management (retain, or simply assign, depending on how you've configured your property).

They are also a means of providing a clean separation between the public interface of your class and it's internal implementation.

This article offers some good advice on when and why to use properties and dot-notation.

Luke Redpath
Yeah but if I have something that is only used internally in my class, then there is no need to use @property, right?
Dimitris
No, you don't have to, at that point I'd say it was purely a matter of style/preference. Just remember to retain/release in all the right places.If you're using NIBs, its probably worth reading up on how IBOutlet works with properties and memory management too.
Luke Redpath
If something is conceptually a @property, but private to your class, you can declare the @property as private in a class continuation.
Jim Correia
@Dimitris Even if you use a property only internally, at the very least, it will save you a lot of additional typing and lead to more manageable code. For instance, let's say you need to set an ivar to a mutable reference passed as an argument, instead of writing: if (myIvar != theirIvar) { [myIvar release]; myIvar = [theirIvar copy]; } you can simply write self.myIvar = theirIvar; (in which case the autogenerated setter will automatically take care of the memory management etc.)
Nocturne
Makes sense. Which leads to the following question though: If @property is so cool, why doesn't the compiler handle everything I declare in my header this way? :) In any case, thanks for the clarifications.
Dimitris
+1  A: 

It also allows you to use the dot syntax:

self.myProperty = something;

ennuikiller
Dot syntax can be used for any accessor style message. It doesn't require formal declaration as @property.
Jim Correia
true but the question applies equally to all accessor methods. He questions the use of accessors if he doesn't plan on exposing his properties. This answer indicates the value of accessor methods when this is the case.
ennuikiller
A: 

I don't know that particular example. However, it is considered good style to access members of the same class through accessors rather than referencing the members directly. By encapsulating the members as a property with getters and setters, the implementation details of the field may change while those details are abstracted by the getter/setter. Furthermore, the declaration of properties on the class allows you to use the .-notation to access the properties so it might lead to more consistent code if you want to use that notation.

VoidPointer