objective c - public objects and use of property -


i'm bit confused; if object declared in .h file considered automatically "public" right? use @property in .h file, however, edit them? don't understand: use getter/setter private objects, why use @property objects declared in .h file , considered "public"?

second thing, found example: don't understand why use @synthesize primarykey in code: http://staging.icodeblog.com/wp-content/uploads/2008/08/9-todom1.png , why don't use @property database object?

it not correct if object (ivar) declared in .h file, public. if getter/setter methods provided, otherwise not.

indeed, @property/@synthesize directives facilities meant declare , define default getter/setter methods. so, instead of writing them yourself, use directives.

it worth noting declaring properties possibility of using dot notation refer properties of objects. , clarify lot, retain/assign/copy specifiers, how memory meant managed properties. (and, of course, @synthesize correctly you).

about sample, in fact, whether ivar associated property or not design choice. possibly, reconsider assumption ivars declared in .h files public defaults, , become clearer. in other words: primarykey public, database not.

a nice tutorial can found here not forget apple docs.

edit:

about question comment section:

it not necessary every ivar has property, nor has getter/setter in order used inside of class implementation.

@interface someclass : nsobject {     anotherclass* _anotherclassobj;     athirdclass* _athirdclassobj; } @property (nonatomic, retain) anotherclass* anotherclassobj; @end 

so, here have 2 ivars; 1 has got @property declaration. in .m file may have, e.g.

@implementation someclass; @synthesize anotherclassobj = _anotherclassobj;  - (void)initwithclasses:(anotherclass*)obj1 and:(athirdclass*)obj2 {    .....    self.anotherclassobj = obj1;    _athirdclassobj = obj2;    ... }  .... @end 

in code:

  1. @synthesize provide implementation getter/setter anotherclassobj can use syntax: self.anotherclassobj = obj1; syntax can used equally inside , outside class implementation;

  2. when have no getter/setter (either auto-generated or custom) can assign directly ivar using syntax _athirdclassobj = obj2;, semantics of simple pointer copy; anyway, _athirdclassobj not accessible outside class;

  3. furthermore, @property ... anotherclassobj notwithstanding, can still refer _anotherclassobj directly in .m file, in _anotherclassobj = xxx, bypassing getter/setter, if ever need it.

one thing should have clear getter/setter not way make ivar "public". play important role in managing retain count (depending on specifier choose among retain/assign/copy in property declaration). so, in self.anotherclassobj = obj1; above, obj1 assigned _anotherclassobj , retained (and if _anotherclassobj pointing object, object sent release). raw ivar assignment not provide kind of facility.

in opinion, retain count management feature of properties far more important visibility deciding whether use property or not.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -