I’m a bit confused; if an object is declared in the .h file it is considered automatically as “public” right? We use a @property in the .h file, however, to edit them? This is where I don’t understand: we use the getter/setter for private objects, so why do we use the @property for objects declared in the .h file and thus considered as “public”?
Second thing, I found this example: I don’t understand why we use a @synthesize for primaryKey in this code: http://staging.icodeblog.com/wp-content/uploads/2008/08/9-todom1.png
and why we don’t use a @property for the database object?
It is not correct that if an object (ivar) is declared in a .h file, then it is public. It is only if getter/setter methods are provided, otherwise it is not.
Indeed, the
@property/@synthesizedirectives are facilities meant to declare and define default getter/setter methods. So, instead of writing them yourself, you just use the directives.It is also worth noting that declaring properties you get the possibility of using the dot notation to refer properties of your objects. And also that they clarify a lot, thanks to the
retain/assign/copyspecifiers, how memory is meant to be managed for that properties. (And, of course,@synthesizewill just do that correctly for you).About your sample, in fact, whether an ivar is associated to a property or not is a design choice. Possibly, you just reconsider the assumption that ivars declared in .h files are public by defaults, and it will become clearer. In other words:
primaryKeyis public,databaseis not.A very nice tutorial can be found here but also do not forget Apple docs.
EDIT:
about your question from the comment section:
it is not necessary that every ivar has a property, nor that it has getter/setter in order to be used inside of that class implementation.
So, here you have two ivars; only one has got a
@propertydeclaration. In your .m file you may have, e.g.In this code:
@synthesizewill provide implementation for getter/setter foranotherClassObjso you can use syntax:self.anotherClassObj = obj1; that syntax can be used equally from inside and outside the class implementation;when you have no getter/setter (either auto-generated or custom) you can assign directly to an ivar by using the syntax
_aThirdClassObj = obj2;, with the semantics of simple pointer copy; anyway,_aThirdClassObjwill not accessible from outside that class;furthermore,
@property ... anotherClassObjnotwithstanding, you can still refer_anotherClassObjdirectly in your .m file, like in_anotherClassObj = xxx, bypassing getter/setter, if you ever need it.One thing you should have clear is that getter/setter are not only a way to make an ivar “public”. They also play an important role in managing the retain count (depending on which specifier you choose among retain/assign/copy in the property declaration). So, in
self.anotherClassObj = obj1;above,obj1is assigned to_anotherClassObjand it is also retained (and if_anotherClassObjwas previously pointing to an object, that object will be sent arelease). Raw ivar assignment does not provide that kind of facility.In my opinion, the retain count management feature of properties is far more important than visibility for deciding whether I use a property or not.