public objects and use of property
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
/@synthesize
directives 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
/copy
specifiers, how memory is meant to be managed for that properties. (And, of course, @synthesize
will 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: primaryKey
is public, database
is 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.
@interface SomeClass : NSObject {
AnotherClass* _anotherClassObj;
AThirdClass* _aThirdClassObj;
}
@property (nonatomic, retain) AnotherClass* anotherClassObj;
@end
So, here you have two ivars; only one has got a @property
declaration. In your .m file you may have, e.g.
@implementation SomeClass;
@synthesize anotherClassObj = _anotherClassObj;
- (void)initWithClasses:(AnotherClass*)obj1 and:(AThirdClass*)obj2 {
.....
self.anotherClassObj = obj1;
_aThirdClassObj = obj2;
...
}
....
@end
In this code:
@synthesize
will provide implementation for getter/setter foranotherClassObj
so 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,_aThirdClassObj
will not accessible from outside that class;furthermore,
@property ... anotherClassObj
notwithstanding, you can still refer_anotherClassObj
directly 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, obj1
is assigned to _anotherClassObj
and it is also retained (and if _anotherClassObj
was previously pointing to an object, that object will be sent a release
). 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.
Not everything in the header is public, by default ivars (items in the { }) are @protected. The purpose of the @property is data encapsulation. @synthesize or @dynamic is used for declaring the way you want to implement your property and one or the other is necessary to prevent crashes and warnings.
Resources:
Defining Classes @protected, @package, @private, @public reference
Declared Properties @property reference
精彩评论